What if you could protect your data like a secret diary that only lets others read or write when you want?
Why Read-only and write-only properties in C Sharp (C#)? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a notebook where you want to keep some information secret or only allow others to read it but not change it. If you write everything openly, anyone can change or see what they shouldn't.
Manually checking every time if someone is allowed to read or write data is slow and easy to forget. This can cause mistakes like unwanted changes or exposing private information.
Read-only and write-only properties let you control who can see or change data easily. You can make some information visible but not changeable, or allow changes but keep it hidden.
private string secret;
public string GetSecret() { return secret; }
public void SetSecret(string value) { secret = value; }public string Secret { get; private set; } // read-only outside
public string Password { private get; set; } // write-only outsideThis makes your code safer and clearer by controlling data access simply and effectively.
Think of a bank account balance you want people to see but not change, or a password you want to set but never show.
Read-only properties allow data to be seen but not changed.
Write-only properties allow data to be changed but not seen.
They help protect and control access to important information in your code.
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
