SoFunction
Updated on 2025-03-06

Introduction to the usage of out parameters, ref parameters and params variable parameters in C#

out parameter:

out keywords are passed through references. Out keywords must be used when defining methods and calling methods.

Simply put, out can be used to return multiple parameter types.

       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

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

The difference between ref parameter and out: out must be assigned a value inside the definition method, and ref must be assigned a value to its parameter before calling the method.

        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;
        }

Note that when defining the method, you don't need to return the value~

params variable parameters

All elements in the actual parameter list that are consistent with the mutable parameter array type are treated as elements of the array.

Params variable parameters must be the last element of the formal parameter.

        static void Main(string[] args)
        {
            //Method 1: You can use array to pass parameters            //int[] scores = {22,11,33};
            //test("Zhang San",11,scores)            //Method 2: You can also use elements that are consistent with the array type when calling directly when calling            test ("Zhang San", 100, 22, 11, 33);
            ();
        }
        /// <summary>
        /// params test function to calculate the total score of a student        /// When using params, it must be placed in the last parameter, as shown below!        /// </summary>
        /// <param name="name">name</param>        /// <param name="number">Student number</param>        /// <param name="s">Variable numbers to make up the grade</param>        public static void test(string name, int number, params int[] s)
        {
            int sum = 0;
            for (int i = 0; i &lt; ; i++)
            {
                sum = sum + s[i];
            }
            ("{0}The student number is{1},Total score is{2}", name, number, sum);
        }

This is all about this article about the usage of out parameters, ref parameters and params variable parameters in C#. I hope it will be helpful to everyone's learning and I hope everyone will support me more.