We know that int is a value type and so the following makes sense:
int x = 3;
int y = x;
y = 5;
Console.WriteLine(x); //says 3.
Now, here is a
You can do something like this:
public delegate void Setter(T newValue);
public delegate T Getter();
public class MagicPointer
{
private Getter getter;
private Setter setter;
public T Value
{
get
{
return getter();
}
set
{
setter(value);
}
}
public MagicPointer(Getter getter, Setter setter)
{
this.getter = getter;
this.setter = setter;
}
}
usage:
int foo = 3;
var pointer = new MagicPointer(() => foo, x => foo = x);
pointer.Value++;
//now foo is 4
Of course this solution doesn't guarantee a strong compile time control, because is up to the coder to write a good getter or setter.
Probably, if you need something like a pointer you should reconsider your design, because likely you can do it in another way in C# :)