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

Properties vs fields in C Sharp (C#) - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Properties vs fields
Start
Declare field
Declare property
Access field directly
Access property (get/set)
Field stores data
Property controls access
End
This flow shows how fields store data directly, while properties provide controlled access to that data.
Execution Sample
C Sharp (C#)
class Person {
  public string name; // field
  private int _age;   // field
  public int Age {    // property
    get { return _age; }
    set { if (value >= 0) _age = value; }
  }
}
Defines a class with a public field and a property that controls access to a private field.
Execution Table
StepActionField 'name'Field '_age'Property 'Age' (get/set)Output/Effect
1Create Person objectnull0N/AObject created, fields default initialized
2Set name = "Alice""Alice"0N/AField 'name' set directly
3Set Age = 30"Alice"30set called with 30Property setter checks and sets _age
4Get Age"Alice"30get returns 30Property getter returns _age value
5Set Age = -5"Alice"30set called with -5Setter ignores invalid negative value
6Get Age"Alice"30get returns 30Age remains 30, no change
7Access name directly"Alice"30N/ADirect access to field value
8Access Age via property"Alice"30get returns 30Access controlled by property
9End"Alice"30N/AExecution ends
💡 Execution stops after demonstrating field direct access and property controlled access.
Variable Tracker
VariableStartAfter 2After 3After 5Final
namenull"Alice""Alice""Alice""Alice"
_age00303030
Age (property)N/AN/Aset=30set=-5 ignoredget=30
Key Moments - 3 Insights
Why doesn't setting Age to -5 change the _age field?
Because the property setter checks if the value is negative and ignores it if so, as shown in step 5 of the execution_table.
Can we access the private field _age directly from outside the class?
No, _age is private. We access it only through the Age property, which controls how it is read or changed (see steps 3 and 4).
Why do we use properties instead of public fields?
Properties let us add rules when getting or setting values, like validation or extra logic, unlike fields which store data directly (compare step 2 vs step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What happens when Age is set to -5?
AThe setter ignores the value and _age stays 30
BThe _age field is updated to -5
CAn error is thrown
DThe name field changes
💡 Hint
Check the 'Action' and 'Output/Effect' columns at step 5 in execution_table.
At which step does the property getter return the current age value?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look for 'get returns' in the Property 'Age' column in execution_table.
If we remove the property setter validation, what would happen at step 5?
AThe name field would change
BThe _age field would be set to -5
CThe _age field would remain 30
DThe program would crash
💡 Hint
Refer to variable_tracker and how the setter controls _age changes.
Concept Snapshot
Fields store data directly and are accessed without checks.
Properties provide controlled access with get and set methods.
Use properties to add validation or logic when reading/writing data.
Fields can be public or private; properties usually control private fields.
Access properties like fields but they run code behind scenes.
Full Transcript
This lesson shows the difference between fields and properties in C#. Fields hold data directly and can be accessed or changed without restrictions. Properties act like smart fields: they let you control how data is read or changed by using get and set methods. For example, a property can prevent invalid values from being stored. The execution table traces creating a Person object, setting a public field 'name' directly, and using the Age property to safely set and get a private field '_age'. When trying to set Age to a negative number, the property setter ignores it, keeping the data valid. This helps beginners see why properties are useful for controlling access and adding rules, unlike fields which are simple storage.