C # The method of parameter passing mechanism

C # The method of parameter passing mechanism

Depending on the parameter passing mechanisms, in the form of parameter C # methods into four categories:Value parameter, a reference parameter, an output parameter, and a parameter arrayBy adding different parameter when the parameter is defined to represent descriptor.

A, the value parameter :(Parameter Type Value form)
Without modifiers parameter is a value parameter declaration.
A parameter value corresponding to a local variable in the process, but its initial value from the corresponding arguments of the method call is provided.
When the parameter is a parameter value, the argument must be implicitly convertible to the parameter of the method call in the corresponding requirements.

See exchange a number of examples, when the Exchange method, a and b in the Main and unaffected by:

 	class Program
    {
        static void Main(string[] args)
        {
            int a = 3, b = 5;
            Console.WriteLine("Main.. a:" + a + "...b:" + b);
            Exchange(a, b);
            Console.WriteLine("Main.. a:" + a + "...b:" + b);
        }

        static void Exchange(int a, int b)
        {
            Console.WriteLine("Exchange.. a:" + a + "...b:" + b);
            int temp = a;
            a = b;
            b = temp;
            Console.WriteLine("Exchange.. a:" + a + "...b:" + b);
        }
    }

result


Second, the reference parameter (the ref keyword)
Declared with a ref modifier parameter is a reference parameter.
That argument storage positions represented by location reference parameter is given method call.
When the parameter for the reference parameters, the method calls the corresponding argument must be connected to a same parameter type and the keyword variable composition after ref. Variable before it can be used as a reference parameter transfer must be definitely assigned.

Also in the above code is an example of a method parameter I ref modified look Results: The values ​​of a and b in this case also occur in the Main method of conversion.

	class Program
    {
        static void Main(string[] args)
        {
            int a = 3, b = 5;
            Console.WriteLine("Main.. a:" + a + "...b:" + b);
            Exchange(ref a,ref b);
            Console.WriteLine("Main.. a:" + a + "...b:" + b);

            Console.ReadKey();
        }

        static void Exchange(ref int a,ref int b)
        {
            Console.WriteLine("Exchange.. a:" + a + "...b:" + b);
            int temp = a;
            a = b;
            b = temp;
            Console.WriteLine("Exchange.. a:" + a + "...b:" + b);
        }
    }

result

That is, when we use reference parameters, multiple names would indicate the same storage location.

Third, the output parameter (out keyword)
Modifier declared with out parameter is an output parameter.
Output parameter storage location the storage location is represented as the variable given as the argument in the method call.
And use a reference parameter similar to the ref keyword modified to use a modified use the out keyword, except that the reference parameters need assigned before the method call, and the assignment before the output parameters after the method call returns

For chestnut:

 	class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[5] { 1, 2, 3, 4, 5 };
            float average;
            int sum = Calculation(arr, out average);
            Console.WriteLine("这组数的和是:" + sum + "..平均数是:" + average);
            Console.ReadKey();
        }

        static int Calculation(int[] arr,out float average)
        {
            int sum = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                sum += arr[i];
            }
            average = sum / arr.Length;
            return sum;
        }
    }

result

Output parameters can be used to transfer data from the caller method.

Fourth, the shape parameters (Reference type parameter form)
When the method parameter type is a reference type, the value of the parameter to be modified, the original variable is modified, the type that is referenced by passing parameters, is passed into the address.

For chestnut: modifying the parameter values ​​in the array, the original array will be modified, look at the code:

 	class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[5] { 1, 2, 3, 4, 5 };            
            Console.WriteLine("Main 调用方法前:");
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.WriteLine();
            Reference(arr);
            Console.WriteLine();
            Console.WriteLine("Main 调用方法后:");
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }

            Console.ReadKey();
        }

        static void Reference(int[] arr)
        {
            Console.WriteLine("Reference 传进来:");
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }        
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = 0;
            }
            Console.WriteLine();
            Console.WriteLine("Reference 修改后:");
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
        }
    }

result



In C #Positional parameterswithNamed parameters

Position parameter: When a method call, each argument must correspond a position corresponding to a position parameter, this parameter is called the position parameter, used in the examples above, we are positional parameters.
4.0 starts from C # supports method invocation mechanism named parameters, as long as the parameter name explicitly specified, it may be listed in any order argument in the method call.

I will modify the code example of what form the call, something like this:

	class Program
    {
        static void Main(string[] args)
        {
            int a = 3, b = 5;
            Console.WriteLine("Main.. a:" + a + "...b:" + b);
            //Exchange(a, b);
            Exchange(b: b, a: a);
            Console.WriteLine("Main.. a:" + a + "...b:" + b);

            Console.ReadKey();
        }

        static void Exchange(int a,int b)
        {
            Console.WriteLine("Exchange.. a:" + a + "...b:" + b);
            int temp = a;
            a = b;
            b = temp;
            Console.WriteLine("Exchange.. a:" + a + "...b:" + b);
        }
    }

This example may be written is not very good and should not be formal and actual variable names from the duplicate names, but this influence and function, it can be seen from the following figure when I checked in a parameter, the method will be in a it is identified, is the same represents a variable, then the Main methoda: A logo is also out, so that he and this parameter is the same variable

parameter

PS: named parameters, you can clearly know that a variable assignment of that argument, which makes it less prone to error when the parameters have more method calls, increase readability of the code.
Published 475 original articles · won praise 682 · views 520 000 +

Guess you like

Origin blog.csdn.net/Czhenya/article/details/104914423