Differences between ref and out

Source: Internet
Author: User

Preface

I have been training at the company for the past few days, and I have some basic knowledge. At the same time, I also took this opportunity to consolidate my own infrastructure. The foundation is too important. I have been looking at multithreading for some time. Next I will write some other things. After all, as an intern, I must be close to my own practice, however, I feel that I have not learned much about multithreading in the past. I will continue to use multithreading in the future ,,,

During yesterday's training, I had a question about the difference between ref and out. I don't know what it means. I only know that they can be used for reference transfer. There is no concept for the difference, it seems that Mr. Yin, who taught us how to use C # as a beginner, mentioned that this can be used for a digital exchange. However, this was the only thing at the time. Let's take a look at it today.

First, I would like to declare that this article references some of the content of the online articles, and then, based on my own understanding and summary, there is something wrong, even though it is made a brick.

About ref

I often hear about the value type and reference type. In essence, it is a storage address for storing the value. Let's look at the following code:

                           Test(              .num =                         x =  Test(                   Main(              Test ts =  Test(         }

What do you think will be the output of the above Code? 10? 100? The correct result is 10. I guess 100 is definitely in doubt ,,,

Next, I will change the two locations, so I will not copy the entire one. I will paste the two locations, as shown below:

Ts. ChangeNum (Test x) // modify the above 8th rows

I have to discuss the results again, 10? 100? The correct result is 100.


Above, the addresses of the original code variables ts and x are on the left, and the addresses of ts and x after the two locations are modified on the right. we can clearly see the ts address (0x05f0eb2c) on the left) unlike the x address (0x05f0eacc), the ts address (0x0568ee6c) in the right image is the same as the x address (0x0568ee6c. Because ts in the Main function in the original code is not the same variable as x in Test, the Code executed in the following 18 lines will not affect the num value, the following two lines of code are different. Because the code points to the same variable, the result is changed to 100.

About

Let's take a look at a piece of code. The fact is pretty good. Haha:

                           Test(              .num =            ChangeNum(              x =  Test(                   Main(              Test ts =  Test(              ts.ChangeNum(         }

The two results of the above Code Execution and printing are also different. Print 10 and 100 respectively. The principle is almost the same as above. Does it feel a bit like the ref mentioned above, so with the title "differences between ref and out" today, let's talk about the differences!

Differences Between out and ref

As can be passed as a reference, the difference is that the value is passed. The ref must be initialized before being passed, and the out parameter does not need to be initialized. Even Initialization is ignored. The out parameter can be used as an output parameter to solve the problem that the function has only one return value.

           Main(              Program pg =                           pg.Method_1(               y =              pg.Method_2(               Method_1(               x = ;                   Console.WriteLine(             Method_2(               x++             Console.WriteLine(         }

Running result:

For a bit of nostalgia, when Mr. Yin talked about a = 5 and B = 10, a and B exchanged values, that is, the ref

           yy(  x,                            temp =             x =             y =            Main(              Program pg =               a =               b =              Console.WriteLine( + a +  +             pg.yy( a,             Console.WriteLine(+a++          }

Then the above Code is implemented with out:

           yy( a, b,  x,               x =             y =            Main(              Program pg =               a =               b =              Console.WriteLine( + a +  +             pg.yy(a,b, a,             Console.WriteLine(+a++          }

Self-feeling: in many places, ref and out can be exchanged and play the same role. I personally feel that ref can be used more freely. out is always awkward. After all, you must assign values internally.

The first weekend of internship

It seems that this article is not worth mentioning, but in general it is related to the main content of the essay. Next, due to the related training content, we have to shift the focus, you have to go back and check out the functions of Linq and EF. The habit of sleeping late is still changing. This is a problem to be solved. After listening to the song for one night, the single loop "wants to sing me to you". It's too late, go to bed !!!

------If you think this article is helpful to you, don't forget to click the recommendation in the lower right corner. Thank you!------

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.