Thursday, 5 September 2013

Passing a reference by reference

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

No comments:

Post a Comment