C # Didi: Out And ref

Source: Internet
Author: User

I have been using C # For a long time, but basically I don't know what it is. When C is used as a single-chip computer, I really think C is more friendly.

However, it is always necessary to make progress and accumulate slowly. This time, we will write a can PC template, and we will say goodbye to the Serial PC in future projects.

Let's talk about the out and ref, which are the pointer function parameters in the C language.

// In C language, pass void test_function (int * temp) {* temp = 100 ;}

This is why C # is used today. It is really a tragedy that only ref or out can be used.

First, Baidu:

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.

C # routine:

// C # // outvoid test_function_1 (Out int temp) {// The temp has been cleared, that is, you cannot use temp to assign values to other variables. // before leaving the function, temp must assign a value of temp = 100;} // call // you do not need to initialize int temp_1; test_funton_1 (Out temp_1 );
// C # // refvoid test_function_2 (ref int temp) {// The temp has been initialized before the call. // The temp can be left unchanged before the function is called, you can also modify temp = 100;} // call // You must initialize int temp_2 = 0; test_funton_2 (ref temp_2 );

Bytes ------------------------------------------------------------------------------------------

Author: PANG Hui

Source: http://www.cnblogs.com/pang123hui/

This article is based on the signature 2.5 mainland China license agreement. You are welcome to repost, interpret, or use it for commercial purposes. However, you must keep the signature Pang Hui (including the link) of this 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.