Passing a reference by reference

不问归期 提交于 2019-12-11 01:56:56

问题


How could it be any useful to pass a reference object by reference. The regular usage is as the following:

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 would anybody write the following, which does the same thing and does print: "Marc 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";
}

回答1:


It doesn't do the same thing.. under the hood.

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

Think of references as memory pointers. student has the value 1134.. a memory address. When you don't use ref, 1134 is applied to a new reference.. pointing at the same memory address.

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

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"

Whereas, 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 reference. Without it.. you're just telling a different reference to point somewhere else (which is void once the function steps out). So, when using ref, you're giving the callee the ability to physically change the reference itself.. not just the memory it points at.




回答2:


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




回答3:


Passing a reference by reference allows you to not only edit the referenced data, but also allows you to change the object it is referring to itself.

The difference is in these uses:

// from the Main's point of view, this function does absolutely nothing
public static PutLastName(Student student)
{
    student = new Student();
}

// This would clear Main's student.
public static PutLastName(ref Student student)
{
    student = new Student();
}

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




回答4:


Basically, passing by reference (ref or out) does the same thing regardless of type (reference type or other types) -- it allows an assignment to the parameter in the function to have the same effect as an assignment to the original passed variable in the calling scope. This can never happen when not passing by reference.



来源:https://stackoverflow.com/questions/18649089/passing-a-reference-by-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!