C # ref vs. out keyword parsing

Source: Internet
Author: User

Summary: REF and Out are keywords that are frequently used in C # development, so as one. NET development, you must know how to use these two keywords.

1, the same point

Both ref and out are passed by address, and the values of the original parameters are changed after use.

2. Ref keyword

(1), note points using the REF keyword:

I, method definitions, and calling methods must explicitly use the REF keyword

II, parameters passed to the ref parameter must be initialized, or the program will error

III, through this attribute of ref, to some extent, solves the problem that the function in C # can only have one return value.

(2), code example:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsoleapplication1{classProgram {Static voidMain (string[] args) {            intA =6; intb = the; Fun (refArefb); Console.WriteLine ("A:{0},b:{1}", A, b);//output: 72 and 6 Description The incoming fun method is a and B reference        }        Static voidFun (ref intAref intb) {a= A+b;//72, indicating the value of a and B of the main method is passed in.b =6; }    }}

(2), out keyword

(1), using the Out keyword points of attention:

I, method definitions and calling methods must explicitly use the Out keyword

The Out keyword cannot pass the parameter value to the method where the out parameter is located, but only the reference to the parameter (personal understanding), so the parameter value initialization of the out parameter must be done within its method, or the program will error

III, through the out of this feature, to some extent, solves the C # function can only have a return value of the problem

(2), code example

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsoleapplication1{classProgram {Static voidMain (string[] args) {            intA= -; intb; Fun ( outA outb); Console.WriteLine ("A:{0},b:{1}", A, b);//output: 3 and 1 indicate that the out parameter is passed in a and B references, and the output 3 indicates that the parameter value of a is not passed into the fun method        }        Static voidFun ( out intA out intb) {a=1+2; b=1; }    }}

(3), ref and out differences

The main differences between ref and out through the above parsing are:

Ref passes the parameter values and references of the parameters to the method, so the initialization of the parameters of ref must be done outside the method, that is, the ref parameter must have an initialization value, or the program will error

Out does not pass the parameter value of the argument into the method, only the reference to the parameter is passed into the method, so the initialization of the parameter must be done in its own method, or the program will error

(4), ref and out use should be noted

I

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsoleapplication1{classProgram { Public voidSampleMethod (ref inti) {} Public voidSampleMethod ( out inti) {} }}

Although ref and out are handled differently at run time, they are handled the same way at compile time. Therefore, if one method takes a ref parameter and the other method takes an out parameter, the two methods cannot be overloaded. For example, from a compilation point of view, the two methods in the following code are identical, so the above code will not be compiled

Ii

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsoleapplication1{classProgram { Public voidSampleMethod (inti) {} Public voidSampleMethod (ref inti) {} }}

However, if a method takes a ref or out parameter and the other method does not take both parameters, it can be overloaded

C # ref vs. out keyword parsing

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.