What is the diff between Ref And Out?

Possible duplicate:
What is the difference between the keywords 'ref' and 'out'?

What is the difference between ref and out ? I am confused when to use ref and out . Therefore, please explain how to use ref and out , and in what situations.

+4
source share
5 answers
  • You use Ref when passing the initialized parameter and expect the method / function to change it.
  • You use Out when passing an uninitialized parameter, and the method will have to initialize and populate this parameter (you will receive a warning or even an error otherwise).

    bool IsUserValid (username of the string);

    void IsUserValid (row username, out bool valid);

The above ads are about the same. It is easier to return a value, so in this case you will use the return type. But if your method also needs to return the user's date of birth, you cannot return both parameters to return, you must use the parameters to return one of them (or cancel the method and return both from it).

+14
source

One thing you should pay attention to is when (not) using "ref" with parameters of the reference type. "Ref" refers to the link itself, and not to the contents of the object that the link points to.

If you pass the link โ€œby valueโ€ (that is, without โ€œrefโ€ or โ€œoutโ€), you cannot change the link (so the โ€œnewโ€ cannot survive the call), you can still change the values โ€‹โ€‹of the properties to which this link indicates (if the class allows this).

+2
source

what's the best way to show you the provision of an MSDN link ;)

from this link:

The difference between ref and out is subtle but important . Each parameter transfer mode is intended for use in slightly different programming scenarios. An important difference between the out and ref parameters is the specific assignment rules used by each.

+1
source

He generally did not approve of the use (or abuse) and correction, often much cleaner to return a structure or a simple class containing several fields that you need to "return".

As for ref vs. out, out requires an uninitialized variable, and the code will not compile unless you set the out parameter before exiting the function.

So the code below is not :

 bool TryParse(string text, out int result) { if (text == null) return false; else { // do the parsing } } 

ref does not require installation. Also, as Hans mentions, you can actually be a โ€œnewโ€ object of reference type when using ref (since you get a link to a link that is approximately equal to a pointer to an object ** in C ++)

+1
source

Indeed, there are 3 ways to pass a parameter to a method: by reference, by value, and as output.

By default, it does not have a keyword in C # (it works in VB.Net: ByVal ) - it passes a copy of value types:

 public void SomeMethod1(int num) { num = 2; } int myNum = 1; SomeMethod1( myNum ); // myNum is still 1, we just set a new one 

Vaguely - by value is passed a copy of the link for reference types. This means that your changes in the reference type are reverted to the instance, but you only have a copy of the actual pointer to the link:

 public void SomeMethod1(MyClass instance) { // changes the name on the instance instance.Name = "test 1"; // we're only nulling the copy passed to this method instance = null; } public void SomeMethod2(MyClass instance) { // changes the name on the instance instance.Name = "test 2"; // this is a new instance only in this method instance = new MyClass { Name = "new instance" }; } MyClass myInst = new MyClass { Name = "original" }; SomeMethod1( myInst ); // myInst.Name is now "test 1" SomeMethod2( myInst ); // myInst.Name is now "test 2" 

So, now the link to the value for structs is passed by reference ( ref in C # or ByRef in VB.Net):

 public void SomeMethod1(ref int num) { num = 2; } int myNum = 1; SomeMethod1( ref myNum ); // myNum is now 2, we changed the reference 

Simple enough, but for reference types, the actual pointer to the instance passes through the link, not the copy:

 public void SomeMethod1(ref MyClass instance) { // changes the name on the instance instance.Name = "test 1"; // we're nulling the reference passed to this method instance = null; } public void SomeMethod2(ref MyClass instance) { // changes the name on the instance instance.Name = "test 2"; // this is a new instance replacing the original instance = new MyClass { Name = "new instance" }; } MyClass myInst = new MyClass { Name = "original" }; SomeMethod1( ref myInst ); // myInst.Name will now throw a NullReferenceException because myInst is null SomeMethod2( ref myInst ); // myInst.Name is now "new instance" 

Thus, although both by reference and by value are similar for reference types, the behavior is very different if you change the link itself (and not what you mean).

Finally, an additional return variable is used as the output, as is the actual return. These two base values โ€‹โ€‹are the same:

 public int SomeMethod1() { return 1; } public void SomeMethod2(out int num) { num = 1; } 

If you have an out parameter, it must be populated with a method (like returning).

0
source

Source: https://habr.com/ru/post/1286450/


All Articles