Value type and reference type problem
Hi I'm trying to do a simple swap of two objects.My code is
void Main()
{
object First = 5;
object Second = 10;
Swap(First, Second);
//If I display results it displays as
//Value of First as 5 and Second as 10
}
private static void Swap(objec开发者_运维百科t First, object Second)
{
object temp = First;
First = Second;
Second = temp;
}
Since objects are reference type, its reference should be passed to method and it should swap. why is it not happening?
Look at it this way.
Let's rename the parameters in your swap method as "x" and "y", because making them the same as the locals is just confusing.
You have an apple and an orange. Two objects.
You have two pieces of paper that say "APPLE" and "ORANGE". Those are references to the objects.
You have two boxes, one labelled First and one labelled Second.
You put "APPLE" in First and "ORANGE" in second.
Now you obtain two more boxes labelled x and y.
You make a photocopy of APPLE and put it in x. You make a photocopy of ORANGE and put it in y.
Now you swap the contents of x and y. APPLE and ORANGE are still references, but you didn't change the contents of First and Second. You changed the contents of x and y.
Now suppose you add "ref" to the declarations of parameters x and y. This now means:
You have two boxes, one labelled First and one labelled Second.
You put "APPLE" in First and "ORANGE" in second.
You add a new label to box First; it may now also be called x. And Second may also be called y.
You swap the contents of x and y. Since they are just aliases for First and Second, the contents of First and Second are swapped.
Make sense?
There's various different things here:
- the objects live off in the heap. Forget about them for now
- the references to the objects are what get stored in
First
and second - you have two variables in
Main
- you have two parameters in
Swap
now; the important thing is the difference between "reference type / references", and "pass by reference". They are completely unrelated.
in the line:
Swap(First, Second);
you pass the values of two variables to Swap
. In this case, the value of First
/ Second
is the reference to the boxed object.
Next;
private static void Swap(object First, object Second)
{
object temp = First;
First = Second;
Second = temp;
}
Here, you swap the value of two local parameters, but these are completely independent to anything else. If we want the caller to see the change to the values (i.e. reassignment), we need to pass by reference:
private static void Swap(ref object First, ref object Second)
{
object temp = First;
First = Second;
Second = temp;
}
Now the value of First
is no longer a reference to the boxed object; it is a reference to a reference to the boxed object. At the caller, we use:
Swap(ref First,ref Second);
which means pass the reference of variable First
, rather than pass the value of variable First
.
Note that I said you could forget about the fact that there is an object? Everything above is exactly the same if we used:
int x = 1, y = 2;
Swap(ref x, ref y);
void Swap(ref int a, ref int b) {
var tmp = a; a = b; b = tmp;
}
the only difference is that the value of x
is 1 etc, and ref x
is a reference to variable x. In pass by reference, reference-type vs value-type is completely irrelevant; the only important thing is understanding that you pass the value of a variable by default, where the value of a variable is 1
(etc), or "a reference to an object". Either way, the logic is the same.
The object pointed to by the parameters inside the Swap
method are references to the First
and Second
objects inside Main
, but the parameters themselves are local to the Swap
method.
So if you inside Swap
had written First = 1; Second = 2
;, you would have seen the change in the objects inside Main
. However, you only change what the paremeters to Swap
point to (by assigning them to another object), and do not change the objects at all. The same would be true if you tried setting the objects to null
inside the Swap
method.
You are not passing it as a reference, you need to explicitly state that you are passing the reference:
Swap(ref First,ref Second);
private static void Swap(ref object First,ref object Second)
You're passing the reference to the objects by value (pass by value is the default in C#).
Instead, you need to pass the objects to the function by reference (using the ref
keyword).
private static void Swap(ref object first, ref object second)
{
object temp = first;
first = second;
second = temp;
}
void Main()
{
object first = 5;
object second = 10;
Swap(ref first, ref second);
}
Of course, if you're using C# 2.0 or later, you would do better to define a generic version of this function that can accept parameters of any type, rather than the object
type. For example:
private static void Swap<T>(ref T first, ref T second)
{
T temp;
temp = first;
first = second;
second = temp;
}
Then you could also declare the variables with their proper type, int
.
Rather than passing the references to the objects by value you need to pass them by reference. In other words, you need to pass a reference to the reference to the object.
This is achieved using the ref
keyword.
E.g.
private static void Swap(ref object First, ref object Second)
{
object temp = First;
First = Second;
Second = temp;
}
You need to pass by reference.
Here's some extra info on pass-by-refence, pass-by-value: http://www.yoda.arachsys.com/csharp/parameters.html, try this:
void Main()
{
object First = 5;
object Second = 10;
Swap(ref First, ref Second);
//If I display results it displays as
//Value of First as 5 and Second as 10
}
private static void Swap(ref object First, ref object Second)
{
object temp = First;
First = Second;
Second = temp;
}
精彩评论