Properties let you control how data is accessed or changed in a class. Read-only properties let you see data but not change it. Write-only properties let you change data but not see it.
Read-only and write-only properties in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
public class MyClass { // Read-only property public int ReadOnlyProperty { get; } // Write-only property private int _writeOnlyField; public int WriteOnlyProperty { set { _writeOnlyField = value; } } }
A read-only property has only a get accessor.
A write-only property has only a set accessor.
public class Person { private int _age; // Read-only property public int Age { get { return _age; } } public Person(int age) { _age = age; } }
public class Secret { private string _password; // Write-only property public string Password { set { _password = value; } } }
public class Example { private int _value; // Read-only property with expression body public int Value => _value; // Write-only property public int SetValue { set { _value = value; } } }
This program creates a person with an age you can read but a password you can only set. It shows how read-only and write-only properties work.
using System; public class Person { private int _age; private string _password; // Read-only property public int Age { get { return _age; } } // Write-only property public string Password { set { _password = value; } } public Person(int age) { _age = age; } public void ShowPassword() { Console.WriteLine("Password is hidden and cannot be read directly."); } } class Program { static void Main() { Person p = new Person(30); Console.WriteLine($"Age: {p.Age}"); p.Password = "mySecret123"; p.ShowPassword(); // The following line would cause a compile error because Password has no get accessor // Console.WriteLine(p.Password); } }
Trying to read a write-only property or write to a read-only property will cause a compile error.
Read-only properties are useful for data you want to protect from changes after setting.
Write-only properties are rare but useful for sensitive data like passwords.
Read-only properties have only a get accessor to allow reading but not writing.
Write-only properties have only a set accessor to allow writing but not reading.
They help control how data is accessed and protect sensitive information.
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
