Showing posts with label c# min and max method. Show all posts
Showing posts with label c# min and max method. Show all posts

Sunday, April 24, 2016

Max and min Methods in c#



Max Method – Max method return larger value, max methods receives two numbers and return larger of the two arguments. The main advantage of using max method is that we can replace many if-statements with one line of code.

Example : -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] ar = { 2, -2, -3, 0 };
            Console.WriteLine(ar.Max());
            Console.ReadKey();

        }
    }
}

Output : 2
Min Methods – Min method is used to find the minimum value from the collection .The example of the min method is given below :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] ar = { 2, -2, -3, 0 };
            Console.WriteLine(ar.Min());
            Console.ReadKey();

        }
    }
}

Output : -3