Recall & Review
beginner
What is a read-only property in C#?
A read-only property is a property that allows you to get (read) its value but not set (write) it. It has a getter but no setter.
Click to reveal answer
beginner
What is a write-only property in C#?
A write-only property is a property that allows you to set (write) its value but not get (read) it. It has a setter but no getter.
Click to reveal answer
beginner
How do you declare a read-only property in C#?
You declare a read-only property by providing only the get accessor and omitting the set accessor. Example: <br>
public int Age { get; }Click to reveal answer
intermediate
Can a property be both read-only and write-only at the same time?
No, a property cannot be both read-only and write-only simultaneously because it would mean you can neither read nor write it. It must have at least one accessor.
Click to reveal answer
intermediate
Why would you use a write-only property?
You use a write-only property to allow setting a value without allowing it to be read back. This can be useful for sensitive data like passwords where you want to accept input but not expose the value.
Click to reveal answer
Which accessor(s) does a read-only property have?
✗ Incorrect
A read-only property has only the get accessor to allow reading the value.
What happens if you try to read a write-only property?
✗ Incorrect
Trying to read a write-only property causes a compile-time error because there is no get accessor.
How do you declare a write-only property in C#?
✗ Incorrect
A write-only property has only the set accessor to allow writing the value.
Why might you want a property to be read-only?
✗ Incorrect
Read-only properties allow reading but prevent external code from changing the value.
Which of these is a valid read-only property declaration?
✗ Incorrect
A read-only property has only the get accessor, like 'public int Value { get; }'.
Explain how to create a read-only property in C# and why you might use it.
Think about properties that should not be changed after creation.
You got /4 concepts.
Describe a scenario where a write-only property is useful and how to declare it.
Consider when you want to accept input but not reveal it.
You got /4 concepts.