Possible Duplicate:
C# : Why doesn't 'ref' and 'out' support polymorphism?
I can't seem to understand why the following does not work in C#:
public static void Swap(ref Object a, ref Object b) {
Object t = b;
b = a;
a = t;
}
//Calls
String fname = "Ford";
Strinf lname = "James";
Swap(ref lname, ref fname);
Is this because String already references to a char array, its immutable?
This is a duplicate of
Why doesn't 'ref' and 'out' support polymorphism?
See that answer for why it doesn't work.
To make it work, you could make a generic method:
public static void Swap<T>(ref T a, ref T b)
{
T t = b;
b = a;
a = t;
}
And now all the types check out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With