C # method parameter passing mechanism

Source: Internet
Author: User

1:value (value pass), ref (reference pass), out (output pass)

The effects of ref and out are equivalent, and the difference is that the parameter is marked ref, then the value of the parameter must be initialized before the function is called, the parameter is marked out, and the function is not required to initialize the object before it is called, but the function called will need to assign a value to the object before the function returns.

Using System;//value (value pass), ref (reference pass), out (output pass) public class test{public    static void Valuefun (int i)    {        i++;    }    public static void Reffun (ref int J)    {        j + +;    }    public static void Outfun (out int k)    {        k = 0;//uses the Out keyword and must have parameters initialized        k++;    }    static void Main ()    {        int i = 0;        Valuefun (i); the value of//i is a copy of the argument, and argument I does not change        Console.WriteLine (i);        int j = 0;        Reffun (ref J);//points to the same piece of memory, the value of argument J changes        Console.WriteLine (j);        int k;        Outfun (out k);//and ref keyword equivalent        Console.WriteLine (k);}    }

2:params variable Parameters

The main use of the params is to pass arguments to the function when the parameters of the function are not fixed.

No additional arguments are allowed after the params keyword in the method declaration, and only one params keyword is allowed in the method declaration .

For the parameter array, you need to know the following points:

(1) If a parameter array is included in the formal parameter list, the parameter array must be at the end of the formal parameter

(2) The parameter array must be a one-dimensional array

(3) The params modifier is not allowed to be combined with the ref and out modifiers.

(4) The argument corresponding to the parameter array can be an array name of the same type, or any number of variables of the same type as the elements of the array

(5) If an argument is an array, it is passed by reference, if the argument is a variable or an expression is passed by value

(6) Usage: Variable method parameters, also known as array parameters, suitable for the number of parameters of the method is unknown, used to pass a large number of array parameter; When using an array parameter, you can specify multiple method parameters in the formal parameter list by using the params keyword, and specify an array in the parameter table of the method. Form: Method modifier returns type method name (params type [] variable name)

The using system;//variable quantity parameter//keyword params must be followed by an array of public class test{    static int addint (params int[] values)    {        int sum = 0;        foreach (int i in values)        {            sum + = i;        }        return sum;    }    static void Main ()    {        int[] arr = {1,2,3,4,5,6,7,8,9};        Console.WriteLine (AddInt (arr));}    }
Transferred from: http://www.cnblogs.com/ArmyShen/archive/2012/08/27/2657899.html

C # method parameter passing mechanism

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.