How do you look at out and ref in C #? Explore the differences between them

Source: Internet
Author: User
Tags apache php php mysql
In the study of C #, there are always some language commands such as the use of similar but not the same, today's discussion is Out and ref in C #The difference between the first discussion is the respective usage, and below I will summarize the difference between out and ref. Apache PHP MySQL

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

Ref can pass the value of the parameter into the function, but out is to empty the parameter, that is, you cannot pass a value out of the out, after the out, the value of the parameter is empty, so you must 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.

For example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

classRefExample

{

staticvoid Method(ref inti)

{

i = 44;

}

staticvoidMain()

{

intval = 0;

Method(refval);

// val is now 44

}

}

The arguments passed to the ref parameter must be initialized first. This differs from out, whose arguments do not need to be explicitly initialized before being passed.

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 following code will not be compiled:

1

2

3

4

5

6

7

classCS0663_Example

{

// Compiler error CS0663: "cannot define overloaded

// methods that differ only on ref and out".

publicvoid SampleMethod(ref inti) { }

publicvoid SampleMethod(out inti) { }

}

However, if a method takes a ref or out parameter and the other method does not take both parameters, it can be overloaded, as shown in the following example:

1

2

3

4

5

classRefOutOverloadExample

{

publicvoid SampleMethod(inti) { }

publicvoid SampleMethod(ref inti) { }

}

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

classOutExample

{

staticvoid Method(out inti)

{

i = 44;

}

staticvoidMain()

{

intvalue;

Method(outvalue);

// value is now 44

}

}

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.

The ref and out keywords are handled differently at run time, but 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 following code will not be compiled:

1

2

3

4

5

6

7

classCS0663_Example

{

// Compiler error CS0663: "Cannot define overloaded

// methods that differ only on ref and out".

publicvoid SampleMethod(out inti) { }

publicvoid SampleMethod(ref inti) { }

}

However, if a method takes a ref or out parameter and the other method does not take these two types of arguments, it can be overloaded as follows:

1

2

3

4

5

classRefOutOverloadExample

{

publicvoid SampleMethod(inti) { }

publicvoid SampleMethod(out inti) { }

}

Here are some of my experiences:

Difference

The difference between ref and out is that in C #, parameters can be passed either by value or by reference. Passing parameters by reference allows a function member to change the value of a parameter and keep the change. To pass parameters by reference, you can use the ref or out keyword. The two keywords, ref and out, can provide similar effects and act like pointer variables in C. The difference between them is:

1. When using a ref parameter, the passed-in parameter must first be initialized. For out, initialization must be done in the method.

2. When using ref and out, the ref or out keyword is added to the method's parameters and execution methods. To meet the match.

3. Out is suitable for use where multiple return values need to be retrun, while ref is used to modify the caller's reference in a method that needs to be called.

The Out method parameter keyword on the method parameter causes the method reference to be passed to the same variable of the method. When control is passed back to the calling method, any changes made to the parameter in the method are reflected in the variable.

Declaring an out method is useful when you want a method to return more than one value. Methods that use out parameters can still return a value. A method can have more than one out parameter.

To use an out parameter, you must explicitly pass the parameter to the method as an out parameter. The value of the out parameter is not passed to the out parameter.

You do not have to initialize a variable passed as an out parameter. However, you must assign a value to the out parameter before the method returns.

property is not a variable and cannot be passed as an out parameter.

In short, in a nutshell is: Ref is in and out, and out is only out.

Related articles:

C # Tutorial C # data type

Viewing the characteristics of c,c++,c#,java,php by static local variables

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.