Using the 'ref' keyword in C #

Possible duplicates:
Why use the ref keyword when passing an object?
When to pass the ref keyword in

What is the proper use of the 'ref' keyword in C #. I believe that this was a lot of discussion topics, but for me it is not clear:

  • Is the ref keyword required if you are passing a reference object? I mean, when you create an object on the heap, is it not always passed by reference. Should it be explicitly marked as a link?
+4
source share
4 answers

Using ref means the link is passed to the function.

The default behavior is that the function receives a new reference to the same object. This means that if you change the value of the link (for example, set it to a new object), you will no longer point to the original source object. When you pass with ref , changing the value of the link changes the original link - because it is one and the same.

Consider this:

 public class Thing { public string Property {get;set;} } public static void Go(Thing thing) { thing = new Thing(); thing.Property = "Changed"; } public static void Go(ref Thing thing) { thing = new Thing(); thing.Property = "Changed"; } 

Then if you run

 var g = new Thing(); // this will not alter g Go(g); // this *will* alter g Go(ref g); 
+20
source

The answers here are a lot of confusing misinformation. The easiest way to understand this is to abandon the idea that "ref" means "by reference." The best way to think about what โ€œrefโ€ means is โ€œI want this formal parameter on the callerโ€™s side to be an alias for a specific variable on the callerโ€™s sideโ€.

When you speak

 void M(ref int y) { y = 123; } ... int x = 456; M(ref x); 

which says "during a call to M, the formal parameter y on the called side is another name for the variable x on the calling side." Assigning 123 to y is exactly the same as assigning 123 to x because they are the same variable , a variable with two names.

It's all. Do not think about reference types or value types or anything else; don't think about passing by reference or passing by value. All "ref" means "temporarily make a middle name for this variable."

+14
source

I believe that the ref keyword indicates that you are passing an object by reference, not by value. For instance:

 void myfunction(ref object a) { a = new Something(); } 

will change the value of a in the calling function however

 void myfunction(object a) { a = new Something(); } 

will change the value locally, but not in the calling function. You can still change the properties of an object, but you cannot set the value of the element itself. For instance; a.someproperty = value;

will work in both cases.

0
source
 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; namespace InOutRef { static class InOutRef { public static void In(int i) { Console.WriteLine(i); i=100; Console.WriteLine(i); } public static void Ref(ref int i) { Console.WriteLine(i); i=200; Console.WriteLine(i); } public static void Out(out int i) { //Console.WriteLine(i); //Error Unsigned Ref i=300; Console.WriteLine(i); } } class Program { static void Main(string[] args) { int i = 1; InOutRef.In(i); //passed by value (in only) Debug.Assert(i==1); InOutRef.Ref(ref i); //passed by ref (in or out) Debug.Assert(i == 200); InOutRef.Out(out i); //passed by as out ref (out only) Debug.Assert(i == 300); } } } 

I can not be more literal in my answer. When used , the code will not remember reference groups, such as the classic Java question exchange. However, when using ref, it will be similar to VB.NET, as it will remember the changes in and out . If you use the out parameter, it means that it must be declared before returning (this is done by the compiler).

  Output:
 1 // 1 from main 
 100 // 100 from in
 1 // 1 is NOT remembered from In
 200 // 200 from ref
 // should be 200 here but out enforces out param (not printed because commented out)
 300 // 300 is out only
 Press any key to continue.  .  .
0
source

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


All Articles