Essentially what you want is a pointer to the value type, but in the
case of a value type I'm not sure what that means. I'll explain. Before
I do, though, have you noticed that your code doesn't do what you'd
like it to?
class A
{
bool refval;
A(ref bool val)
{
refval = val;
}
B()
{
refval = true;
}
}
In this class, the line
reval = val;
just copies the value of the bool from "val" into "refval". It doesn't
_store a pointer_ to the value that was passed into A(). So, when you
call B(), all it does is update the "refval" bool to contain something
different. This is _not_ the same as the old C way of doing it:
int *refvalp;
void A(int *valp)
{
refvalp = valp;
}
void B(void)
{
*refvalp = true;
}
because in the C case you're holding a _pointer_ to an int, whereas in
the C# case you're holding a bool, not a pointer (or a reference), and
there's no way to declare a class member like this:
private bool ref refval;
....in other words, in C# there's no way to declare a pointer.
Think of what would happen if you could. Let's say that your class does
what you would like: it stores a "pointer" to an int and then allows
you to update it later. What if your class instance outlives the int?
Consider this:
public void CrashAndBurn()
{
A = GetA();
UseA(A);
}
public A GetA()
{
bool x;
return new A(ref x);
}
public void UseA(A anA)
{
int i;
anA.B();
}
The line anA.B() would be following the "pointer" it holds and updating
the bool "x", but "x" is a local variable of the method GetA(), which
has finished and released its stack space... stack space that is now
being used by UseA to hold its int, i. So, calling anA.B() would slam a
bool onto the stack where "x" _was_, and in so doing corrupt "i".
Instant stack corruption. Easy end run around all of the security that
..NET offers.
That's why C# doesn't allow pointers in safe code.
Now, that said, this doesn't mean that there aren't ways to hold
references to particular properties of particular instances allocated
on the heap. You can do that using Reflection. You cannot, however,
hold a reference to an arbitrary value type and then update it whenever
you like.