0
0
C Sharp (C#)programming~10 mins

Get and set accessors in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Get and set accessors
Start
Create object
Access property
Get accessor runs
Set accessor runs
End
This flow shows how a property is accessed: reading calls the get accessor to return a value, writing calls the set accessor to update the value.
Execution Sample
C Sharp (C#)
class Person {
  private string name;
  public string Name {
    get { return name; }
    set { name = value; }
  }
}

var p = new Person();
p.Name = "Anna";
Console.WriteLine(p.Name);
This code creates a Person object, sets the Name property to "Anna" using the set accessor, then reads it using the get accessor and prints it.
Execution Table
StepActionAccessor CalledVariable 'name' ValueOutput
1Create Person objectNonenull
2Set p.Name = "Anna"setAnna
3Get p.NamegetAnnaAnna printed
💡 Program ends after printing the Name property value.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
namenullnullAnnaAnna
Key Moments - 2 Insights
Why does setting p.Name call the set accessor and not the get accessor?
Because in the execution_table at Step 2, the action is setting the property, so the set accessor runs to update the private variable.
What happens if you try to get p.Name before setting it?
The get accessor returns the current value of 'name', which is null initially, as shown in Step 1 where 'name' is null.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'name' after Step 2?
Anull
B"" (empty string)
C"Anna"
Dundefined
💡 Hint
Check the 'Variable 'name' Value' column at Step 2 in the execution_table.
At which step is the get accessor called?
AStep 3
BStep 2
CStep 1
DNever
💡 Hint
Look at the 'Accessor Called' column in the execution_table.
If you remove the set accessor, what happens when you try to assign p.Name = "Anna"?
AIt works normally.
BCompilation error because property is read-only.
CThe get accessor runs instead.
DThe private variable 'name' updates automatically.
💡 Hint
Properties without set accessors are read-only; assignment causes error.
Concept Snapshot
Get and set accessors define how a property reads and writes data.
Get accessor returns the value.
Set accessor updates the value using 'value' keyword.
Accessing property calls get or set automatically.
Useful to control access to private data.
Full Transcript
This example shows a class Person with a private variable 'name' and a public property Name with get and set accessors. When we create a Person object, 'name' starts as null. Setting p.Name to "Anna" calls the set accessor, which updates 'name' to "Anna". Getting p.Name calls the get accessor, which returns "Anna". The execution table tracks these steps and variable changes. Beginners often wonder why set runs on assignment and get on reading; this is shown clearly in the steps. Also, if the set accessor is missing, assigning to the property causes a compile error because the property is read-only.