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

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main difference between a field and a property in C#?
easy
A. Properties store data directly, fields control access to data.
B. Fields and properties are exactly the same in C#.
C. Fields store data directly, properties control access to data.
D. Properties can only be used in structs, fields only in classes.

Solution

  1. Step 1: Understand what a field does

    A field is a variable inside a class that holds data directly.
  2. Step 2: Understand what a property does

    A property provides controlled access to data, often using get and set methods.
  3. Final Answer:

    Fields store data directly, properties control access to data. -> Option C
  4. Quick Check:

    Field = direct data, Property = controlled access [OK]
Hint: Fields hold data; properties manage access to it. [OK]
Common Mistakes:
  • Confusing fields and properties as the same.
  • Thinking properties store data directly.
  • Believing fields control access like properties.
2. Which of the following is the correct syntax to declare a property named Age with a private field in C#?
easy
A. int Age; int Age { get; set; }
B. private int Age; public int Age { get; set; }
C. public int age; public int Age;
D. private int age; public int Age { get { return age; } set { age = value; } }

Solution

  1. Step 1: Identify private field declaration

    The private field should be lowercase (e.g., age) and declared as private int age;.
  2. Step 2: Identify property syntax

    The property Age uses get and set to access the private field correctly.
  3. Final Answer:

    private int age; public int Age { get { return age; } set { age = value; } } -> Option D
  4. Quick Check:

    Private field + property with get/set = correct syntax [OK]
Hint: Private field lowercase, property uppercase with get/set. [OK]
Common Mistakes:
  • Using same name for field and property causing errors.
  • Missing get or set in property.
  • Declaring fields as public when they should be private.
3. What will be the output of this C# code?
class Person {
  private string name = "Alice";
  public string Name {
    get { return name; }
    set { name = value; }
  }
}

var p = new Person();
p.Name = "Bob";
Console.WriteLine(p.Name);
medium
A. Bob
B. name
C. Alice
D. Compilation error

Solution

  1. Step 1: Understand property set operation

    The line p.Name = "Bob"; calls the set accessor, changing the private field name to "Bob".
  2. Step 2: Understand property get operation

    The line Console.WriteLine(p.Name); calls the get accessor, returning the updated value "Bob".
  3. Final Answer:

    Bob -> Option A
  4. Quick Check:

    Property set changes value, get returns updated value [OK]
Hint: Property set changes field; get returns updated value. [OK]
Common Mistakes:
  • Assuming output is original field value.
  • Confusing field name with property name.
  • Thinking code causes compilation error.
4. Identify the error in this code snippet:
class Car {
  public int speed;
  public int Speed {
    get { return speed; }
    set { speed = value; }
  }
}

var c = new Car();
c.Speed = 50;
Console.WriteLine(c.speed);
medium
A. Accessing field directly breaks encapsulation.
B. Field and property have the same name causing conflict.
C. No error; code works fine.
D. Property must be static to access field.

Solution

  1. Step 1: Check field and property names

    The field is speed and property is Speed, so no naming conflict.
  2. Step 2: Analyze direct field access

    Accessing c.speed directly bypasses the property, which can break encapsulation and safety.
  3. Final Answer:

    Accessing field directly breaks encapsulation. -> Option A
  4. Quick Check:

    Use properties to protect data, not direct field access [OK]
Hint: Avoid direct field access; use properties for safety. [OK]
Common Mistakes:
  • Thinking same names cause error (case-sensitive).
  • Believing direct field access is always safe.
  • Assuming property must be static.
5. You want to create a class BankAccount where the Balance can be read publicly but only changed inside the class. Which is the best way to declare Balance?
hard
A. public decimal Balance; // public field
B. private decimal balance; public decimal Balance { get; private set; }
C. public decimal Balance { private get; set; }
D. private decimal Balance; public decimal balance { get; set; }

Solution

  1. Step 1: Understand access needs

    Balance should be readable publicly but only settable privately inside the class.
  2. Step 2: Choose property with private set

    Using public decimal Balance { get; private set; } allows public reading but restricts setting to inside the class.
  3. Final Answer:

    private decimal balance; public decimal Balance { get; private set; } -> Option B
  4. Quick Check:

    Public get + private set = controlled access [OK]
Hint: Use property with private set for controlled write access. [OK]
Common Mistakes:
  • Using public field exposes data to unwanted changes.
  • Setting private get hides data from outside.
  • Confusing field and property naming conventions.