0
0
C Sharp (C#)programming~10 mins

Read-only and write-only properties in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Read-only and write-only properties
Define Property
Is it Read-only?
YesOnly get accessor
Can read value
Is it Write-only?
YesOnly set accessor
Can set value
Has both get and set
Can read and write value
This flow shows how a property can be defined as read-only (only get), write-only (only set), or both (get and set).
Execution Sample
C Sharp (C#)
class Person {
  private string name;
  public string Name { get => name; } // Read-only
  private string secret;
  public string Secret { set => secret = value; } // Write-only
}
Defines a class with a read-only property 'Name' and a write-only property 'Secret'.
Execution Table
StepActionProperty AccessedOperationResult/Effect
1Create Person instance--Person object created with default fields
2Set Name propertyNamesetError: No set accessor, cannot assign
3Get Name propertyNamegetReturns current 'name' value (default null)
4Set Secret propertySecretsetSets 'secret' field to assigned value
5Get Secret propertySecretgetError: No get accessor, cannot read
6Add set accessor to NameNamesetNow can assign value to 'name'
7Set Name propertyNameset'name' field updated
8Get Name propertyNamegetReturns updated 'name' value
💡 Execution stops after demonstrating read-only and write-only property access and errors.
Variable Tracker
VariableStartAfter Step 4After Step 7Final
namenullnull"Alice""Alice"
secretnull"mySecret""mySecret""mySecret"
Key Moments - 3 Insights
Why does setting the read-only property 'Name' cause an error?
Because 'Name' only has a get accessor (see step 2 in execution_table), so it cannot be assigned a value.
Why can't we read the write-only property 'Secret'?
Because 'Secret' only has a set accessor (see step 5), so reading it is not allowed and causes an error.
What changes when we add a set accessor to 'Name'?
Now 'Name' can be assigned a value (step 6 and 7), allowing both reading and writing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens when we try to set the 'Name' property at step 2?
AThe 'name' field is updated
BAn error occurs because there is no set accessor
CThe 'secret' field is updated
DThe property returns null
💡 Hint
Check step 2 in the execution_table where setting 'Name' causes an error.
According to variable_tracker, what is the value of 'secret' after step 4?
Anull
B"Alice"
C"mySecret"
DError
💡 Hint
Look at the 'secret' row after step 4 in variable_tracker.
If we remove the get accessor from 'Name', what would happen when trying to read it at step 8?
AIt causes an error because get accessor is missing
BIt returns the current value
CIt sets the value instead
DIt returns null always
💡 Hint
Refer to step 5 in execution_table where missing accessor causes error.
Concept Snapshot
Read-only property: has only get accessor, can only read value.
Write-only property: has only set accessor, can only assign value.
Both get and set: property can be read and written.
Trying to access missing accessor causes compile-time error.
Use private fields to store property data.
Full Transcript
This example shows how properties in C# can be read-only or write-only. A read-only property has only a get accessor, so you can read its value but cannot assign to it. A write-only property has only a set accessor, so you can assign a value but cannot read it. Trying to use a missing accessor causes an error. Adding both get and set allows reading and writing. The variable tracker shows how the underlying fields change as properties are accessed or assigned.