Difference between out and ref in C #

Source: Internet
Author: User

 

In chapter 3 of Section 5.0, we use the extraction method in refactoring to extract some functional code into a method. Sometimes the OUT keyword is added when the parameter is passed by address. What is the difference between him and REF?

Ref is the address for passing parameters, and out is the return value. There are some similarities between the two, but there are also differences.

You must assign values to variables before using ref.

The out function will clear the variable, even if the variable has been assigned a value, it will not work. When exiting the function, all variables referenced by the out function will be assigned a value, and the ref reference can be modified or not modified.

The differences are as follows:

The out type parameter is passed by address, which can change the original value. Before passing an out parameter, the variable can be assigned a value or not assigned a value. A function with an out type parameter clears the variable. Therefore, when you exit the function, all variables referenced by the out type must be assigned a value.

Feature 5: parameters of the ref type are transmitted by address, which can change the original value. Before passing Parameters Using ref, the variable must be assigned a value. Functions with parameters of the ref type do not empty the variables, when you leave this function, all variables referenced by ref can be assigned a value or not assigned a value.

 

See the following code for differences:

Using System;

Class TestApp

{

Static void outTest (out int x, out int y)

{// Assign values to x and y before leaving this function; otherwise, an error is returned.

// Y = x;

// The above line returns an error because after the out is used, both x and y are cleared and need to be assigned a value again, even if the value is assigned before the function is called.

X = 1;

Y = 2;

}

Static void refTest (ref int x, ref int y)

{

X = 1;

Y = x;

}

Public static void Main ()

{

// Out test

Int a, B;

// Before using out, the variable can be assigned no value.

OutTest (out a, out B );

Console. WriteLine ("a = {0}; B = {1}", a, B );

Int c = 11, d = 22;

OutTest (out c, out d );

Console. WriteLine ("c = {0}; d = {1}", c, d );

 

// Ref test

Int m, n;

// RefTest (ref m, ref n );

// The above line will go wrong. Before using ref, the variable must be assigned a value

 

Int o = 11, p = 22;

RefTest (ref o, ref p );

Console. WriteLine ("o = {0}; p = {1}", o, p );

}

}

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.