Passing help by reference

How can it be useful to pass a reference object by reference. Regular use is as follows:

public static void main() { Student st = new Student(); st.FirstName = "Marc"; PutLastName(st); Console.WriteLLine(st.FirstName + " " + st.LastName); } public static PutLastName(Student student) { student.LastName = "Anthony"; } 

Why did someone write the following that does the same and prints: β€œMark Anthony”:

 public static void main() { Student st = new Student(); st.FirstName = "Marc"; PutLastName(ref st); Console.WriteLLine(st.FirstName + " " + st.LastName); } public static PutLastName(ref Student student) { student.LastName = "Anthony"; } 
+2
parameter-passing pass-by-reference c #
source share
4 answers

It does not do the same ... under the hood.

Functionally, it works the same way, yes. Under the hood, though .. the link itself is passed when using ref . Without ref reference value is copied.

Think of references as pointers to memory. student has a value of 1134 .. memory address. If you are not using ref , 1134 applies to the new link. By specifying the same memory address.

Using ref can have dangerous consequences when you understand above. For example, consider the following:

 public static void PutLastName(Student student) { student = new Student(); student.LastName = "Whitehead"; } // .. calling code .. Student st = new Student(); st.FirstName = "Marc"; st.LastName = "Anthony"; PutLastName(st); Console.WriteLLine(st.FirstName + " " + st.LastName); // "Marc Anthony" 

While using ref :

 public static void PutLastName(ref Student student) { student = new Student(); student.FirstName = "Simon"; student.LastName = "Whitehead"; } // .. calling code .. Student st = new Student(); st.FirstName = "Marc"; st.LastName = "Anthony"; PutLastName(ref st); Console.WriteLLine(st.FirstName + " " + st.LastName); // "Simon Whitehead" 

Using ref physically changed the link. Without this .. you just say another link to point somewhere else (which is not valid after exiting the function). Thus, when using ref you give the called party the ability to physically change the link itself ... not just the memory that it points to.

+6
source share

In the second version, ref means PutLastName can change st.

0
source share

Passing a link by link allows you to not only edit the data that it refers to, but also allows you to change the object that it refers to itself.

The difference is in using:

 // from the Main point of view, this function does absolutely nothing public static PutLastName(Student student) { student = new Student(); } // This would clear Main student. public static PutLastName(ref Student student) { student = new Student(); } 

see http://msdn.microsoft.com/en-us/library/14akc2c7.aspx

0
source share

In principle, passing by reference ( ref or out ) does the same regardless of type (reference type or other types) - this allows assigning the parameter in the function the same effect as assigning the original passed variable in the call area. This will never happen unless you follow the link.

0
source share

All Articles