Read-only and write-only properties in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time it takes to use read-only and write-only properties changes as we work with more data.
How does the number of property accesses affect the program's speed?
Analyze the time complexity of the following code snippet.
public class DataHolder
{
private int[] numbers;
public DataHolder(int size)
{
numbers = new int[size];
}
public int this[int index] // Read-only property
{
get { return numbers[index]; }
}
public int WriteOnlyValue // Write-only property
{
set { numbers[0] = value; }
}
}
This code defines a class with a read-only index property to get values and a write-only property to set a value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing an element in the array through the property.
- How many times: Each property access happens once per call; no loops inside the property itself.
Each property access takes about the same time regardless of the array size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 per access |
| 100 | 1 per access |
| 1000 | 1 per access |
Pattern observation: The time to get or set a single element does not grow with the size of the array.
Time Complexity: O(1)
This means each property access takes a constant amount of time, no matter how big the data is.
[X] Wrong: "Accessing a property that works with an array always takes longer if the array is bigger."
[OK] Correct: Accessing a single element by index is direct and does not depend on the array size, so it takes the same time regardless.
Understanding how property access time works helps you explain how your code handles data efficiently, a useful skill in many programming tasks.
"What if the read-only property computed a sum of all elements each time it was accessed? How would the time complexity change?"
Practice
Which statement correctly describes a read-only property in C#?
Solution
Step 1: Understand property accessors
A property with only agetaccessor allows reading but not writing.Step 2: Identify read-only property
Read-only properties have nosetaccessor, so they cannot be assigned a value.Final Answer:
It has only a get accessor and no set accessor. -> Option BQuick Check:
Read-only = get only [OK]
- Confusing read-only with write-only properties
- Thinking both get and set are needed for read-only
- Assuming no accessors means read-only
Which of the following is the correct syntax for a write-only property named Password in C#?
public string Password { ? }Solution
Step 1: Identify write-only property syntax
A write-only property has only asetaccessor to allow writing but no reading.Step 2: Match syntax to write-only
set { _password = value; } shows only asetaccessor with assignment, which is correct for write-only.Final Answer:
set { _password = value; } -> Option AQuick Check:
Write-only = set only [OK]
- Using get accessor in write-only property
- Using both get and set for write-only
- Incorrect accessor visibility modifiers
What will be the output of the following C# code?
class User {
private string _name = "Alice";
public string Name { get { return _name; } }
}
var user = new User();
Console.WriteLine(user.Name);Solution
Step 1: Analyze the property
TheNameproperty is read-only with agetaccessor returning "Alice".Step 2: Check output of Console.WriteLine
Callinguser.Namereturns "Alice", so it prints "Alice".Final Answer:
Alice -> Option DQuick Check:
Read-only property returns stored value [OK]
- Expecting a compilation error due to missing set
- Thinking write-only properties can be read
- Confusing private field with property access
Identify the error in this C# code snippet:
class Account {
private string _pin;
public string Pin {
get { return _pin; }
set { _pin = value; }
}
}
var acc = new Account();
acc.Pin = "1234";
Console.WriteLine(acc.Pin);The goal is to make Pin write-only.
Solution
Step 1: Check property accessors
The property has bothgetandsetaccessors, allowing reading and writing.Step 2: Understand write-only requirement
Write-only properties must have only asetaccessor and nogetaccessor.Final Answer:
The property has both get and set accessors, so it is not write-only. -> Option CQuick Check:
Write-only = set only, no get [OK]
- Leaving both get and set for write-only
- Changing field visibility instead of property
- Expecting set to return a value
You want to create a class Secret that stores a string value. The value should be settable but never readable from outside the class. Inside the class, you want to log the value whenever it is set. Which property implementation achieves this?
Solution
Step 1: Identify write-only property with logging
The property must have only asetaccessor to be write-only and log the value when set.Step 2: Check each option
public string Data { set { Console.WriteLine(value); _data = value; } } has only asetaccessor that logs and assigns the value, matching requirements.Final Answer:
public string Data { set { Console.WriteLine(value); _data = value; } } -> Option AQuick Check:
Write-only with logging in set accessor [OK]
- Adding get accessor making property readable
- Using private get instead of no get
- Not logging inside set accessor
