yazanpro
yazanpro

Reputation: 4752

Passing a reference by reference

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";
}

Upvotes: 2

Views: 209

Answers (4)

newacct
newacct

Reputation: 122429

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.

Upvotes: 0

Simon Whitehead
Simon Whitehead

Reputation: 65049

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.

Upvotes: 6

Nicolas Louis Guillemot
Nicolas Louis Guillemot

Reputation: 1628

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

Upvotes: 0

bazza
bazza

Reputation: 8394

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

Upvotes: 0

Related Questions