What if a simple number in your program could silently cause big bugs? Properties help you stop that!
Properties vs fields in C Sharp (C#) - When to Use Which
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a class representing a person, and you store their age as a simple number. You directly access and change this number everywhere in your code.
Directly changing the age number everywhere can cause mistakes, like setting an impossible age (negative or too high). It's hard to check or control these changes, and bugs sneak in easily.
Properties let you control how values like age are accessed and changed. You can add checks or extra actions automatically, keeping your data safe and your code clean.
public int age; // direct field access
person.age = -5; // no check, wrong valueprivate int age;
public int Age {
get => age;
set {
if (value >= 0) age = value;
}
}
person.Age = -5; // ignored, safeProperties enable safe, controlled access to data, making your programs more reliable and easier to maintain.
In a banking app, you store account balance. Using properties, you prevent setting a negative balance directly, protecting users from errors.
Fields store data directly but offer no control.
Properties let you add rules when getting or setting data.
This control helps prevent bugs and keeps data valid.
Practice
field and a property in C#?Solution
Step 1: Understand what a field does
A field is a variable inside a class that holds data directly.Step 2: Understand what a property does
A property provides controlled access to data, often using get and set methods.Final Answer:
Fields store data directly, properties control access to data. -> Option CQuick Check:
Field = direct data, Property = controlled access [OK]
- Confusing fields and properties as the same.
- Thinking properties store data directly.
- Believing fields control access like properties.
Age with a private field in C#?Solution
Step 1: Identify private field declaration
The private field should be lowercase (e.g.,age) and declared asprivate int age;.Step 2: Identify property syntax
The propertyAgeuses get and set to access the private field correctly.Final Answer:
private int age; public int Age { get { return age; } set { age = value; } } -> Option DQuick Check:
Private field + property with get/set = correct syntax [OK]
- Using same name for field and property causing errors.
- Missing get or set in property.
- Declaring fields as public when they should be private.
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);Solution
Step 1: Understand property set operation
The linep.Name = "Bob";calls the set accessor, changing the private fieldnameto "Bob".Step 2: Understand property get operation
The lineConsole.WriteLine(p.Name);calls the get accessor, returning the updated value "Bob".Final Answer:
Bob -> Option AQuick Check:
Property set changes value, get returns updated value [OK]
- Assuming output is original field value.
- Confusing field name with property name.
- Thinking code causes compilation error.
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);Solution
Step 1: Check field and property names
The field isspeedand property isSpeed, so no naming conflict.Step 2: Analyze direct field access
Accessingc.speeddirectly bypasses the property, which can break encapsulation and safety.Final Answer:
Accessing field directly breaks encapsulation. -> Option AQuick Check:
Use properties to protect data, not direct field access [OK]
- Thinking same names cause error (case-sensitive).
- Believing direct field access is always safe.
- Assuming property must be static.
BankAccount where the Balance can be read publicly but only changed inside the class. Which is the best way to declare Balance?Solution
Step 1: Understand access needs
Balance should be readable publicly but only settable privately inside the class.Step 2: Choose property with private set
Usingpublic decimal Balance { get; private set; }allows public reading but restricts setting to inside the class.Final Answer:
private decimal balance; public decimal Balance { get; private set; } -> Option BQuick Check:
Public get + private set = controlled access [OK]
- Using public field exposes data to unwanted changes.
- Setting private get hides data from outside.
- Confusing field and property naming conventions.
