C # Initial identification of ref and out

Source: Internet
Author: User

First: Both are passed by address and will change the value of the original parameter after use.

Second: Ref can pass the value of the parameter into the function, but out is to empty the parameters, that is, you can not pass a value from out into the, out into the value of the parameter is empty, so you have to initialize it once. This is the difference of two, or as some netizens say, ref is in there, out is only out.

Ref(C # Reference)

The REF keyword enables parameters to be passed by reference. The effect is that when control is passed back to the calling method, any changes to the parameters in the method are reflected in the variable. To use the ref parameter, both the method definition and the calling method must explicitly use the ref keyword. Let's take a look at common communication parameters:
I. Common methods
int n1 = 5;
Myfun (N1);
Console.WriteLine (N1);

public static void Myfun (int i)
{
i = i + 5;
}

No doubt the call output: 5

Second, the method with ref parameter

public static void Myref (ref int i)
{
i = i + 5;
}

Static Void Main ()

{

int n2 = 5;

    int n2; This variable is incorrect for initializing the direct use of ref arguments
Myref (ref n2);
Console.WriteLine (n2);

}

What is this output? The result is 10, because you ref is a reference value that directly alters the value of the N2.

out (C # Reference)

The out keyword causes parameters to be passed by reference. This is similar to the REF keyword, except that the ref requires that the variable be initialized before it is passed. To use an out parameter, both the method definition and the calling method must explicitly use the Out keyword.

public static void Myout (out int i)
{
i = 5 + 5;
}

int n3 = 5;
Myout (out N3);
Console.WriteLine (n3);

Result output: 10

Although a variable passed as an out parameter does not have to be initialized before it is passed, the method needs to be called to assign a value before the method returns.

Iv. methods with params parameters
The params parameter corresponds to a one-dimensional array: int, string, and so on
There can be only one params parameter in the parameter list of a method
When there are multiple parameters, the params parameter must be placed at the end of the parameter list
public static void Myparams (String str, params int[] arr)
{

foreach (int n in arr)
{
Console.WriteLine (n);
}
}

Myparams ("xg", 11, 22);

Output Result: 11,22

usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespacetest{classClass1 {Static voidMain (string[] args) {            intN1 =5; Myfun (N1); //I. Common MethodsConsole.WriteLine (N1); intN2 =5; Myref (refN2);//second, the method with ref parameterConsole.WriteLine (n2); intN3 =5; Myout ( outN3);//three, the method with out parametersConsole.WriteLine (N3); Myparams ("XG", One, A);//Iv. methods with params parametersMyparams ("XG", One, A, -, -); intNumber=5; Modify (refNumber );            Console.WriteLine (number); intNumber1; Testout ( outnumber1);            Console.WriteLine (NUMBER1);        Console.ReadLine (); }        //I. Common Methods         Public Static voidMyfun (inti) {i= i +5; }        //second, the method with ref parameter         Public Static voidMyref (ref inti) {i= i +5; }        //three, the method with out parameters         Public Static voidMyout ( out inti) {i=5+5; }        //Iv. methods with params parameters//the params parameter corresponds to a one-dimensional array: int, string, and so on//there can be only one params parameter in the parameter list of a method//when there are multiple parameters, the params parameter must be placed at the end of the parameter list         Public Static voidMyparams (stringStrparams int[] arr) {            foreach(intNincharr)            {Console.WriteLine (n); }        }         Public Static voidModify (ref intNumber ) { number=1008611; }         Public Static voidTestout ( out inti) {i=9999; }    }}
View Code

C # Initial identification of ref and out

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.