Properties vs fields in C Sharp (C#) - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
We want to see how fast code runs when using properties compared to fields in C#.
How does accessing or setting values change as we do it more times?
Analyze the time complexity of the following code snippet.
public class Sample
{
private int _field;
public int Property { get; set; }
public void UpdateValues(int n)
{
for (int i = 0; i < n; i++)
{
_field = i;
Property = i;
}
}
}
This code sets a private field and a public property inside a loop that runs n times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Assigning values to a field and a property inside a loop.
- How many times: The loop runs n times, so these assignments happen n times.
Each time we increase n, the number of assignments grows the same amount.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 20 assignments (10 to field + 10 to property) |
| 100 | 200 assignments |
| 1000 | 2000 assignments |
Pattern observation: The total work grows directly with n, doubling because of two assignments per loop.
Time Complexity: O(n)
This means the time to run grows in a straight line as the number of loop runs increases.
[X] Wrong: "Accessing a property is always slower and changes the time complexity compared to a field."
[OK] Correct: Both field and property assignments happen once per loop iteration, so time grows linearly for both. The difference is tiny and does not change the overall growth pattern.
Understanding how properties and fields behave in loops helps you explain performance clearly and shows you know how code runs as it scales.
"What if the property had extra logic inside its setter? How would that affect the time complexity?"
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.
