Possible Duplicate:
How do I assign by “reference” to a class field in c#?
I want to copy the reference of my value type to another value type. Is it possible without using unsafe paradigm?
int a = 10;
int b = 15;
a = b; // BUT I WANT TO SET REFERENCE OF b !
Here is my real example. I create enum value type and send it as parameters by reference:
public enum Test
{
test1,
test2,
test3
}
public class MyTestClass1
{
private Test _test;
public MyTestClass1(ref Test test)
{
_test = test;
}
}
public class Content
{
public void Method()
{
Test e = Test.test1;
/*some code*/
var omyTestClass1 = new MyTestClass1(ref e);
/*some code*/
e = Test.test2;
/*some code*/
e = Test.test3;
}
}
What if your method was in fact:
The value returned contains a reference to a value type that had been on the stack, but now isn’t. Now if you try to access that field, you can get anything.
Worse, what if you write to that reference? If that reference had been stored on the actual call-stack, rather than spending all its time in registers (and we are probably safe in assuming that the very fact that there was a field in a class referring to it meant that it had to be there), well what’s there now? It could be a reference to an object. It could be a return address. Writing to it could cause some strange fandango-on-the-core bug, quite possibly some time after the write and hence proving hard to debug.
We’d lose some of the basic guarantees we have. After you’ve written to that value, just about any code anywhere can potentially fail in some bizarre way.
It’s worth noting at this point, that C++ and .NET itself (that is to say, everything you can do in .NET including some you can’t in C#) which both allow local refs and return values of ref don’t allow ref fields.
The closest you can come is with capturing as happens with lambdas and anonymous methods. Here locals aren’t stored on the stack, but in the heap, to allow them to live as long as the lambda they are captured by lives (and get collected when the last of them is collected). You can certainly use this to maintain references to what started as a local, in an object.
Alternatively, you can use a class that does the necessary work to wrap a value type. Here for example is one I used when I had a similar enough need:
I’d a need for some atomicity guarantees that you may not, and likewise maybe didn’t need some things that you do need, but it served perfectly well as a means to be able to deal with the same
intvalue from within different classes.If you don’t mind going through a property each time, you can do a more general-purpose version.
Exposing a field publicly has considerable downsides (which is why we generally never do so), but most of them don’t apply to this case (we want to be able to manipulate it fully from outside) and it means that you can even pass the
Valueas areforoutparameter.