SoFunction
Updated on 2025-03-06

C# output parameter out problem

C# Output parameter out

What are output parameters

When declaring a method, the formal parameters declared using the out modifier become the output parameter.

Features of output parameters

1. The output parameters do not create a new storage location.

2. The storage position represented by the output parameters is the storage position represented by the actual parameter.

3. The actual parameters passed to the output parameters do not need to be forced initialized before the method is called. When using the formal parameter inside the method, it is necessary to force assign once.

Use of out parameters

Using the out parameter, you can make the method return multiple return values.

static void Main(string[] args)
{
   int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
   int max;
   int min;
   int sum;
   double avg;
   int[] arr = GetMaxMinSumAvg(numbers, out max, out min, out sum, out avg);
   (max);
   (min);
   (sum);
   (avg);
   ();
   ();
}
 
public static int[] GetMaxMinSumAvg(int[] nums, out int max, out int min, out int sum, out double avg)
{
   int[] res = new int[4];
   max = ();
   min = ();
   sum = ();
   avg = ();
   return res;
}

Usage of out parameters, ref parameters and value parameters in C#

The ref parameter is a reference, and the out parameter is an output parameter.

out parameter modifier

1. Declaring the out method is very useful when you want the method to return multiple values.

2. There is no need to initialize the variable passed as an out parameter. However, the out parameter must be assigned before the method returns.

3. Attributes are not variables and cannot be passed as out parameters.

 static void Main(string[] args)
        {
            string s = "123";
            int result;
            bool b = MyTest(s,out result);
        }
        public static bool MyTest(string s, out int result)
        {
            bool isTrue;
            try {
                result = Convert.ToInt32(s);//Use out parameters to be assigned within the defined method                isTrue = true;
            }
            catch
            {
                isTrue = false;
                result = 0;
            }
            return isTrue;
        }

This method returns the type of bool type, and while returning the bool type, it also returns the result variable of int type. That is, two variable types are returned.

Ref parameter modifier

1. The initialized variable must be used.

2. Properties are not variables and cannot be passed as ref parameters.

3. Ref is used when the method to be called up and used is modified.

The ref parameter processes it within the defined method and returns the result. The definition method does not require unnecessary return types.

 static void Main(string[] args)
        {
            //Use the ref parameter to exchange the values ​​of two numbers            int a = 1;
            int b = 2;
            Change(ref a, ref b);
            ("a{0},b{1}",a,b);
            ();
        }
        public static void Change(ref int a, ref int b)
        {
            int temp;
            temp = a;
            a = b;
            b = temp;
        }

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.