C # method Parameters Ref out Params 4 types of parameters

Source: Internet
Author: User

Reprint: http://www.cnblogs.com/kissdodog/archive/2013/05/11/3072815.html

I. Passing parameters by value

The value parameter is passed by value to the method by copying The value of the argument to the parameter, which is what we usually say by value.

When the method is called, the CLR does the following:

1. Allocate space for formal parameters in the managed stack;

2. Copy the value of the argument to the formal parameter.

This is too common, passing parameters by value is a copy, so it does not affect the value of the original parameter.

public class program    {        static void Main (string[] args)        {            int i = 1;            int j = 2;            int k = Plus (i,j);            Console.WriteLine (i);   Output 1            Console.WriteLine (j);   Output 2            Console.WriteLine (k);   Output 5            console.readkey ();        }        public static int Plus (int i, int j)        {            i = i + 1;            j = j + 1;            return i + j;        }    }
Ii. passing parameters by reference--keyword ref

Corresponding to the previous "pass by value" is passed by reference. As the name implies, this is not a value, but a reference. Note that this is not a copy, but a way to pass the true self into the method for manipulation.

Note the point:

1. By reference, the system no longer allocates new memory in the managed stack for formal parameters.

2. At this time, the formal parameter name has actually become an alias of the argument name, they point to the same memory location.

    public class program    {        static void Main (string[] args)        {            int i = 1;            int j = 2;            int k = Plus (ref i,ref J);      The REF keyword            Console.WriteLine (i) is also added before the argument.   Output 2            Console.WriteLine (j);   Output 3            Console.WriteLine (k);   Output 5            console.readkey ();        }        public static int Plus (ref int i, ref int J)    //parameter money to add ref keyword        {            i = i + 1;            j = j + 1;            return i + j;        }    }

The above example is almost the same as above, just add 4 ref, notice that in the method of the parameter modification, the method will affect the value of the parameter.

Three, output parameters-keyword out

Output parameters are similar to reference parameters, and output parameters can be used to pass values from within a method to a method, which is actually equivalent to having multiple return values. To use an output parameter, you only need to replace the REF keyword of the reference parameter with the Out keyword. It is important to note, however, that only variables are qualified as output parameters, and neither literal values nor expressions are allowed.

Note Two questions:

1. The compiler allows the value of the reference parameter to be read at any point in the method, at any time.

2. The compiler prohibits reading an output parameter before it is assigned a value.

This means that the initial value of the output parameter is basically meaningless, because it is given a new value before it is used. Therefore, it is not feasible to pass the value to the method through the output parameter.

    public class program    {        static void Main (string[] args)        {            int i = 1;            int j = 2;            int k = Plus (I,out j);      Add the            out keyword Console.WriteLine (i) before the argument.   Output 1            Console.WriteLine (j);   Output of            Console.WriteLine (k);   Output 102            Console.readkey ();        }        public static int Plus (int i, out int j)        {            i = i + 1;            j = +;            return i + j;        }    }
Four, parameter array-keyword params

Parameter array:

public class program    {        static void Main (string[] args)        {            int count1 = Plus (1);       Output 1            Console.WriteLine (count1);            int count2 = Plus (1, 2, 3);//Output 6            Console.WriteLine (count2);            int count3 = Plus ();    The output 0  parameter array itself is optional and does not pass the value without error            {                Console.WriteLine (COUNT3);            }            Console.readkey ();        }        public static int Plus (params int[] values)        {            int count = 0;            foreach (int i in values)            {                count = count + i;            }            return count;        }    }

Additional two C # 4.0 new features optional parameters and named parameters are added:

  1. Optional parameters

Optional parameters, as the name implies, are not required. For general parameters, if you do not specify a value for it, an export run error may occur. However, the optional parameters do not.

Rules for Optional parameters:

1. The optional parameter cannot be the first parameter in the parameter list, it must be after all required parameters;

2, the optional parameter must specify a default value;

3, the default value of the optional parameter must be a constant expression;

4, all parameters after the optional parameters must be optional parameters.

public class program    {        static void Main (string[] args)        {            int count1 = Plus (5);    When an optional parameter is not specified, the default value is            Console.WriteLine (count1);  The output            is Count2 = Plus (5,5), and when an optional parameter is specified, there is a default value of            Console.WriteLine (count2);  Output of            Console.readkey ();        }        public static int Plus (int i, int j = Ten)        {            return i + J;        }    }

  2. Named parameters

The optional parameter solves the problem of parameter defaults, and named parameters solve the problem of parameter order, which frees us from the list of parameters that memory each method. Allows you to enter parameters without order.

public class program    {        static void Main (string[] args)        {            //string str = "string";            int i = ten;            Console.WriteLine (Plus (STR:STR,I:I));     Although it's weird, these 3 lines of code are            Console.WriteLine (Plus (str: "string", I:10))      that works correctly. Note The order is not the same as in the method signature parameter            Console.readkey ();        }        public static string Plus (int i, string str)        {            return str + i.tostring ();        }    }

C # method Parameters Ref out Params 4 types of parameters

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.