SoFunction
Updated on 2025-03-06

c# method to find the maximum value of two numbers

1. Triple operator:

Copy the codeThe code is as follows:

class Program
    {
        static void Main(string[] args)
        {
          int max= NumMAX(10,15);
("Maximum number: {0}",max);
            ();
        }
   /// <summary>
/// The largest value of the two numbers
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        private static int NumMAX(int p1, int p2)
        {
           return p1 > p2 ? p1 : p2;
        }
}

2. If condition judgment:

Copy the codeThe code is as follows:

class Program
    {
        static void Main(string[] args)
        {
          int max= NumMAX(10,15);
("Maximum number: {0}",max);
            ();
        }
   /// <summary>
/// The largest value of the two numbers
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        private static int NumMAX(int p1, int p2)
        {
              if (p1>p2)
            {
                return p1;
            }
            return p2;
        }
}

3. Math function:

Copy the codeThe code is as follows:

class Program
    {
        static void Main(string[] args)
        {
          int max= NumMAX(10,15);
("Maximum number: {0}",max);
            ();
        }
   /// <summary>
/// The largest value of the two numbers
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        private static int NumMAX(int p1, int p2)
        {
           return (p1, p2);
        }
}