Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Reference assignment and shared state in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Reference assignment and shared state
Create Object A
↓
Assign Reference B = A
↓
Modify Object via B
↓
Check Object via A
↓
Both A and B see same changes
When you assign one reference variable to another, both point to the same object. Changes through one reference affect the shared object.
Execution Sample
C Sharp (C#)
class Box {
  public int Value;
}
Box a = new Box();
a.Value = 5;
Box b = a;
b.Value = 10;
Console.WriteLine(a.Value);
This code shows two references pointing to the same object; changing the object via one reference affects the other.
Execution Table
StepActionVariable StatesOutput
1Create new Box object and assign to 'a'a -> Box(Value=0)
2Set a.Value = 5a -> Box(Value=5)
3Assign b = a (both reference same object)a -> Box(Value=5), b -> Box(Value=5)
4Set b.Value = 10 (modifies shared object)a -> Box(Value=10), b -> Box(Value=10)
5Print a.Valuea -> Box(Value=10), b -> Box(Value=10)10
💡 Execution ends after printing the shared object's Value, which is 10.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
anullBox(Value=5)Box(Value=5)Box(Value=10)Box(Value=10)
bnullnullBox(Value=5)Box(Value=10)Box(Value=10)
Key Moments - 2 Insights
Why does changing b.Value also change a.Value?
Because both 'a' and 'b' reference the same Box object (see execution_table step 3 and 4). Changing the object through 'b' affects what 'a' sees.
Does assigning b = a create a new object?
No, it only copies the reference. Both variables point to the same object (execution_table step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of a.Value?
A10
B5
C0
DUndefined
💡 Hint
Check the Variable States column at step 4 where both a and b have Value=10.
At which step do 'a' and 'b' start referencing the same object?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
See the Variable States column; step 3 shows both a and b pointing to the same Box.
If we changed b.Value to 20 instead of 10 at step 4, what would a.Value be at the end?
A5
B20
C10
D0
💡 Hint
Since a and b share the same object, changing b.Value changes a.Value too (see variable_tracker).
Concept Snapshot
Reference assignment means two variables point to the same object.
Changing the object via one reference affects all references.
Assigning one reference to another copies the reference, not the object.
Use this to share and modify data without copying.
Remember: value types behave differently.
Full Transcript
This example shows how reference assignment works in C#. We create a Box object and assign it to variable 'a'. Then we assign 'b' to 'a', so both point to the same object. When we change the Value property through 'b', the change is visible through 'a' because they share the same object. The output prints 10, showing the shared state. This helps understand that reference variables hold addresses to objects, not copies.

Practice

(1/5)
1. What happens when you assign one reference type variable to another in C#?
ClassA a = new ClassA();
ClassA b = a;
easy
A. Both variables point to the same object in memory.
B. A new copy of the object is created for b.
C. The variables become independent and do not affect each other.
D. The program throws a compile-time error.

Solution

  1. Step 1: Understand reference assignment

    Assigning one reference variable to another makes both variables point to the same object in memory.
  2. Step 2: Effect of shared reference

    Changes made through either variable affect the same object, so both see the changes.
  3. Final Answer:

    Both variables point to the same object in memory. -> Option A
  4. Quick Check:

    Reference assignment = shared object [OK]
Hint: Reference variables share the same object after assignment [OK]
Common Mistakes:
  • Thinking a new object is created on assignment
  • Assuming variables become independent
  • Confusing value types with reference types
2. Which of the following is the correct syntax to assign one reference variable to another in C#?
easy
A. int a = b;
B. ClassA a := b;
C. ClassA a == b;
D. ClassA a = new ClassA(); a = b;

Solution

  1. Step 1: Identify correct assignment operator

    In C#, the single equals sign (=) is used for assignment, not double equals (==) or :=.
  2. Step 2: Check variable types and initialization

    ClassA a = new ClassA(); a = b; correctly declares a reference variable and assigns another reference variable to it.
  3. Final Answer:

    ClassA a = new ClassA(); a = b; -> Option D
  4. Quick Check:

    Use = for assignment in C# [OK]
Hint: Use single = for assignment, not == or := [OK]
Common Mistakes:
  • Using == instead of = for assignment
  • Using := which is invalid in C#
  • Assigning incompatible types without casting
3. What is the output of the following code?
class Box { public int Value; }
Box box1 = new Box();
box1.Value = 5;
Box box2 = box1;
box2.Value = 10;
Console.WriteLine(box1.Value);
medium
A. 5
B. 0
C. 10
D. Compilation error

Solution

  1. Step 1: Understand reference assignment effect

    box2 is assigned to box1, so both point to the same Box object.
  2. Step 2: Trace value changes

    Changing box2.Value to 10 changes the shared object, so box1.Value is also 10.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    Shared reference means shared data [OK]
Hint: Changing one reference changes all pointing to same object [OK]
Common Mistakes:
  • Assuming box1.Value stays 5 after box2 changes it
  • Confusing reference and value types
  • Expecting a compile error due to assignment
4. Identify the error in this code snippet:
class Person { public string Name; }
Person p1 = new Person();
p1.Name = "Alice";
Person p2;
p2.Name = "Bob";
p1 = p2;
medium
A. p2 is used before being assigned an object.
B. Cannot assign p2 to p1 because they are different types.
C. Cannot assign string to Name property.
D. No error, code runs fine.

Solution

  1. Step 1: Check variable initialization

    p2 is declared but not assigned an object before accessing p2.Name.
  2. Step 2: Understand runtime error

    Accessing p2.Name causes a NullReferenceException because p2 is null.
  3. Final Answer:

    p2 is used before being assigned an object. -> Option A
  4. Quick Check:

    Uninitialized reference causes runtime error [OK]
Hint: Always initialize reference variables before use [OK]
Common Mistakes:
  • Assuming p2 has default object automatically
  • Confusing compile-time and runtime errors
  • Ignoring null reference exceptions
5. Given this code, what will be the output?
class Data { public int Number; }
Data d1 = new Data() { Number = 1 };
Data d2 = new Data() { Number = 2 };
d1 = d2;
d2.Number = 5;
Console.WriteLine(d1.Number);
hard
A. 2
B. 5
C. 1
D. Compilation error

Solution

  1. Step 1: Analyze reference assignment

    d1 is assigned to d2, so both point to the same object with Number initially 2.
  2. Step 2: Trace value change through d2

    Changing d2.Number to 5 changes the shared object, so d1.Number is also 5.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    Reference assignment shares object state [OK]
Hint: After assignment, variables share the same object state [OK]
Common Mistakes:
  • Thinking d1.Number remains 1 after assignment
  • Confusing value and reference types
  • Expecting compile errors on assignment
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard