Differences between ref and out

Source: Internet
Author: User

It is always known that both ref and out are reference types for passing values. But it's not clear about the differences between them. Today we have a little bit of knowledge.

Everyone knows that if you want to pass an address when passing parameters, you can use the ref or out keyword. In fact, both are transmitted by address, and the original values will be changed after use.
Main differences:
Ref can pass the value of a parameter to the function. However, an out parameter must be assigned a value before it can be used. This overwrites the original value.

In short, there is an inbound/outbound ref, but an outbound ref does not.

For example, in the following two methods, valuewithref does not need to be assigned a value again, but valuewithout will be compiled incorrectly if it is not assigned a value again for I.

  // Normal operation, the output result is still the value of I at the time of input
Static   Void Valuewithref ( Ref   Int I)
{
Console. writeline (I. tostring ());
}
// Error
Static   Void Valuewithout ( Out   Int I)
{
// I = 200; // If this row is commented out, an error is returned.
Console. writeline (I. tostring ());
}

 

 

 

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.