Assign Variable to another Variable and have changes in one be mirrored in the other

You need to distinguish between objects, references and variables. If you have two different variables (which aren't aliased via ref/out in a method, etc) then those will be independent.

However, if two variables refer to the same object (i.e. their type is a class, and their values are equal references), then any changes to that object will be visible via either variable. It sounds like this is what you want to achieve. For example:

public class SomeMutableClass
{
    public string Name { get; set; }
}

// Two independent variables which have the same value
SomeMutableClass x1 = new SomeMutableClass();
SomeMutableClass x2 = x1;

// This doesn't change the value of x1; it changes the
// Name property of the object that x1's value refers to
x1.Name = "Fred";
// The change is visible *via* x2's value.
Console.WriteLine(x2.Name); // Fred

If you're not entirely comfortable with how reference types and objects work, you may wish to read my article about them.

EDIT: One analogy I often use is of a house. Suppose we have two pieces of paper (variables). The same house address is written on both pieces of paper (that's the value of each variable, the reference). There's only one house. If someone uses the first piece of paper to get to the house, then paints the door red, they're not changing anything about their piece of paper - they're changing something about the house. Then if someone uses the second piece of paper to get to the house, they'll see the front door is red too. There's only one house, however many pieces of paper have its address written on them.


With struct/value types ------- NO

like int, boolean, double etc.

With Reference types -------- YES

like classes, objects etc.


First of all you should understand what you are doing in your previous code

  1. Create a 4-byte-sized variable named "a" in memory and after that give it a value of 0
  2. Create a 4-byte-sized variable named "b" in memory and after that give it a value of (a)'s value [Currently 0]
  3. Change the Value of the variable "b" to 1

But this isn't what you want, what you want is something more like this

  1. Create a 4-byte-sized variable named "a" in memory and after that give it a value of 0
  2. Create a 4-byte-sized variable named "b" in memory and after that change the address of the variable "b" so that it doesn't refer to these 4 bytes anymore,but instead refer to (a)'s 4 bytes

What you are asking for is both Bad + Impossible

  • Bad : you have just created an empty space of 4 bytes that have no use at all and completely ignored them
  • Impossible : Because you can't change the address of a value type (thus they are called "value" types not "reference" types)

But what you "Should" be asking for is this

  1. Create a 4-byte-sized variable named "a" in memory and after that give it a value of 0
  2. Create an address that refers to the variable a
  3. Change the value of a through this address

And this approach is actually totally OK, and it uses Pointers, Pointers simply are addresses in memory.

Note : To use pointers you have to Allow unsafe code in Project > Build

In code here is how to do it :

int a = 5;
unsafe //use unsafe around the areas where pointers exist
{
    int* b = &a; //create an address named b that refers to a
    *b = 1; //change the value of the variable that exists at the address b to 1

    //now a = 1
}

Tags:

C#