Unfortunately, there is no native way to store a reference to a variable in C#. Luckily, there are many ways to have b
's X object reference stay in sync with a
's X object reference.
First, the classes:
public class SomeClass
{
public X x;
}
public class X
{
public int i; // I added this int to verify which X object we're working with.
public X(int i)
{
this.i = i;
}
}
Scenario 1: If you need to reference a new X object.
Option 1: If a
and b
need to be different SomeClass objects, you can simply update b
's X object reference to a
's X object reference.
SomeClass a = new SomeClass();
SomeClass b = new SomeClass();
a.x = new X(1);
b.x = a.x;
Console.WriteLine($"a.x.i:{a.x.i}, b.x.i:{b.x.i}"); // a.x.i:1, b.x.i:1
a.x = new X(2);
b.x = a.x;
Console.WriteLine($"a.x.i:{a.x.i}, b.x.i:{b.x.i}"); // a.x.i:2, b.x.i:2
Option 2: If b
doesn't need to be different than a
, have b
reference the same SomeClass object that a
does.
SomeClass a = new SomeClass();
SomeClass b = a;
a.x = new X(1);
Console.WriteLine($"a.x.i:{a.x.i}, b.x.i:{b.x.i}"); // a.x.i:1, b.x.i:1
a.x = new X(2);
Console.WriteLine($"a.x.i:{a.x.i}, b.x.i:{b.x.i}"); // a.x.i:2, b.x.i:2
Scenario 2: If you only need to modify an internal value of the X object.
Since a.x
is the same X object as b.x
, you can modify that object using either reference and view that change using the other reference.
SomeClass a = new SomeClass();
SomeClass b = new SomeClass();
a.x = new X(1);
b.x = a.x;
Console.WriteLine($"a.x.i:{a.x.i}, b.x.i:{b.x.i}"); // a.x.i:1, b.x.i:1
a.x.i = 2;
Console.WriteLine($"a.x.i:{a.x.i}, b.x.i:{b.x.i}"); // a.x.i:2, b.x.i:2
b.x.i = 3;
Console.WriteLine($"a.x.i:{a.x.i}, b.x.i:{b.x.i}"); // a.x.i:3, b.x.i:3
Scenario 3: If you want a different class to always have easy access to a
's X object.
New class:
public class SomeClass2
{
private SomeClass someClass;
public X x { get { return someClass.x; } }
public SomeClass2(SomeClass sc)
{
someClass = sc;
}
}
Using the new class:
SomeClass a = new SomeClass();
SomeClass2 b = new SomeClass2(a);
a.x = new X(1);
Console.WriteLine($"a.x.i:{a.x.i}, b.x.i:{b.x.i}"); // a.x.i:1, b.x.i:1
a.x = new X(2);
Console.WriteLine($"a.x.i:{a.x.i}, b.x.i:{b.x.i}"); // a.x.i:2, b.x.i:2
Scenario 4: If you want a local alias to a.x
.
SomeClass a = new SomeClass();
Func<X> func = () => a.x; // "func()" is now essentially an alias of "a.x"
a.x = new X(1);
Console.WriteLine($"a.x.i:{a.x.i}, func().i:{func().i}"); // a.x.i:1, func().i:1
a.x = new X(2);
Console.WriteLine($"a.x.i:{a.x.i}, func().i:{func().i}"); // a.x.i:2, func().i:2
Scenario 5: If you need to use the same class and have b.x
always reference the same object as a.x
.
You can use a Func<X>
member of your class to act like similarly to a variable reference. This works but I don't necessarily recommend doing it because it is not intuitive and can cause confusion.
The class:
public class SomeClass3
{
public Func<X> x;
}
Using the class:
SomeClass3 a = new SomeClass3();
SomeClass3 b = new SomeClass3();
X x1 = new X(1);
a.x = () => x1;
b.x = () => a.x();
Console.WriteLine($"a.x().i:{a.x().i}, b.x().i:{b.x().i}"); // a.x().i:1, b.x().i:1
X x2 = new X(2);
a.x = () => x2;
Console.WriteLine($"a.x().i:{a.x().i}, b.x().i:{b.x().i}"); // a.x().i:2, b.x().i:2