5.c# Knowledge Points: The ref and out keywords talk about

Source: Internet
Author: User

First we need to know what ref and Out are in C #. A: They are the keywords in C #. What are they two doing? A: They are the modifier of the method parameter, but the use of this keyword is used for both method definitions and methods, which is a dead rule.

Okay, nonsense. Not much, since it is a modifier, it must be meaningful to use them, right? Let's first look at Microsoft's official definition.

If you understand this parameter through the Microsoft official definition above. That's good, I don't need to look at the bottom of my writing! Still have doubts, continue to look down. Make sure you can understand.

Here I do not directly explain, I first took out a hand code, and then through the code to explain.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceref and outtest{classProgram {Static voidMain (string[] args) {            inti =0;            Test (i);            Console.WriteLine (i.ToString ());        Console.readkey (); }        Static voidTest (inti) {i++;                    Console.WriteLine (i.ToString ()); }    }}

This code is not what content, learned the code to understand, is to define an I variable, and then call the test method, the method in the face I do +1 and then print the value of I, the main function is also printed once the value of I.

As a result, you should also know that the method printed in the main method is 0, the method is 1.

because by default, our method passes a variable by default that is the way value is passed. What does that mean? speaking of which, we need to explain the reference type and value type, you can read my blog-----> portal . I'll say it briefly here. The value of a reference type is the address that points to the object itself or to the value, and the variable of the value type is the object itself or the value. A classic metaphor, the value type is the newspaper, the reference type is the Web page. I will buy a newspaper when I read the newspaper. What I get is the actual newspaper, but I browse the page just enter a URL address. The URL points to the actual site content.

After the explanation, we will pull back the idea, we say the method passed the variable by default is "value Pass". That is to say I laughed at the newspaper, so Xiao Ming wanted to see it. I bought another copy for him. But what we are looking at is the same. But my paper is still my newspaper. Xiao Ming read the newspaper and tore the newspaper without affecting me. Value passing is the value of the method's variable after the value is passed. Regardless of the method's operation on the value. Does not affect my way out of the I. So the result of this place is 1 and 0.

I think the above should be very easy to understand, and there are doubts about my failure.

Next we add the REF keyword and change the code

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceref and outtest{classProgram {Static voidMain (string[] args) {            inti =0; Test (refi);            Console.WriteLine (i.ToString ());        Console.readkey (); }        Static voidTest (ref inti) {i++;                    Console.WriteLine (i.ToString ()); }    }}

ref keyword , at the beginning of the article Microsoft's official explanation has been explained. After using the ref parameter, the pass of the method parameter is passed by reference, and the meaning of "reference passing" is explained above. For example, this is like I see a website that is not pleasing to the eye. Just find a hacker, and then put the website address told the hacker, let it help me hacked this site. Let the hacker on the homepage of the website display "One hour little Superman the most handsome word", wants the large number display, that hacker will pass through I to the URL black this website's server. Then I refresh the page. This page shows "one Hour little Superman is the most handsome".

This code is the same. After adding ref I variable pass is the object's reference is also the URL of the Web page, the method internal modification is through this address to find the real value and then modify, all the outside I also changed.

We should understand it here.

both the Out keyword and ref are made to pass a variable by reference. They are the same in this regard. The only thing that doesn't work is. The parameters of ref must be initialized before the method is called. The out parameter must be initialized inside the method. The difference between this point and their application is different.

Scene one has a program is the exchange rate of Chinese renminbi conversion. There are now 10 dollars. To be converted by one method. You should use ref at this time. Because the initial value of this 10 dollar is important, right? I have to define how much my renminbi is in the outside of the method.

Scene two is int. Trypase (parameter 1, out parameter 2) This C # conversion method, with parameter 1, converts the parameter 2 to the int type. At this point, the initial value of the parameter 2 is meaningless, because the result of its final calculation is related to the value of parameter 1, which is not a dime relationship with its initial value. So this place should be used out.

Ok. It's over here, huh? If you have a question about a small partner who has just started learning, you can comment on our study together. If any of Daniel has a random eye on a mistake, please tell me that I can make progress. The code for Out is not, because the two syntax is the same.

5.c# Knowledge Points: The ref and out keywords talk about

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.