OUT usage in C #

Reference: https://www.cnblogs.com/huangxuQaQ/p/10726513.html

1. Out of no examples:

static void Main(string[] args)
        {
            int[] nums = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int[] result = GetMaxMinSumAvg(nums);
            Console.WriteLine("Max-{0},Min-{1},Sum-{2},Avg-{3}", result[0], result[1], result[2], result[3]);
            Console.ReadKey();
        }

        //返回数组中最大值,最小值,总和,平均值,此时4个返回值所以声明一个数组
        public static int[] GetMaxMinSumAvg(int[] nums)
        {
            //假设res[0]最大值,res[1]最小值,res[2]总和,res[3]平均值
            int[] res = new int[4];
            res[0] = nums[0];//max
            res[1] = nums[0];//min
            res[2] = 0;//sum
            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] > res[0])
                {
                    res[0] = nums[i];
                }
                if (nums[i] < res[1])
                {
                    res[1] = nums[i];
                }
                res[2] += nums[i];
            }
            res[3] = res[2] / nums.Length;
            return res;
        }

2.Out Example 1

    The return value type is not at the same time how to do?

    out parameters: a plurality of return value, Any type

        Precautions:

  •   Before calling methods, variables out argument of just a statement, the assignment may or may not be assigned, the assignment will be overwritten in the method
  •   When passing variables out parameter, you must assign a value in the method, otherwise return the return value does not make sense
  •   Parameter method of use out modifications, but also add keywords when you call out the method
  •   Out using modified variables you do not need to return      
static void Main(string[] args)
        {
            int[] nums = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int max;
            int min;
            int sum;
            int avg;
            string s;
            Test(nums, out max, out min, out sum, out avg, out s);
            Console.WriteLine(max);
            Console.WriteLine(min);
            Console.WriteLine(sum);
            Console.WriteLine(avg);
            Console.WriteLine(s);
            Console.ReadKey();
        }

        public static void Test(int[] nums, out int max, out int min, out int sum, out int avg, out string s)//5个out参数修饰的是多余的返回值
        {
            //out参数必须在方法内部为其赋值,否则返回去没有意义
            max = nums[0];
            min = nums[0];
            sum = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] > max)
                {
                    max = nums[i];
                }
                if (nums[i] < min)
                {
                    min = nums[i];
                }
                sum += nums[i];
            }
            avg = sum / nums.Length;
            //此方法void无返回值,无需写return
            s = "Test_Result";
        }

Example 2 3.Out

static void Main(string[] args)
        {
            int n;
            bool b = MyTryParse("123", out n);
            Console.WriteLine(b);
            Console.WriteLine(n);
            Console.ReadKey();
        }

        public static bool MyTryParse(string s, out int result)
        {
            result = 0;
            try
            {
                result = Convert.ToInt32(s);
                return true;
            }
            catch
            {
                return false;
            }
        }

 

Published 60 original articles · won praise 11 · views 60000 +

Guess you like

Origin blog.csdn.net/mianyao1004/article/details/104390700