Bird
Raised Fist0
C Sharp (C#)programming~5 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction

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.

When you want to allow users to get a value but not change it, like a person's age.
When you want to let users set a value but keep it hidden, like a password.
When you want to protect important data from accidental changes.
When you want to control how data is updated or retrieved with extra logic.
When you want to keep some data private but still allow limited access.
Syntax
C Sharp (C#)
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.

Examples
This class lets you read the age but not change it after creation.
C Sharp (C#)
public class Person
{
    private int _age;

    // Read-only property
    public int Age { get { return _age; } }

    public Person(int age)
    {
        _age = age;
    }
}
This class lets you set the password but not read it back.
C Sharp (C#)
public class Secret
{
    private string _password;

    // Write-only property
    public string Password { set { _password = value; } }
}
Shows a short way to write a read-only property and a write-only property.
C Sharp (C#)
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; } }
}
Sample Program

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.

C Sharp (C#)
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);
    }
}
OutputSuccess
Important Notes

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.

Summary

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

(1/5)
1.

Which statement correctly describes a read-only property in C#?

easy
A. It has only a set accessor and no get accessor.
B. It has only a get accessor and no set accessor.
C. It has both get and set accessors.
D. It has no accessors at all.

Solution

  1. Step 1: Understand property accessors

    A property with only a get accessor allows reading but not writing.
  2. Step 2: Identify read-only property

    Read-only properties have no set accessor, so they cannot be assigned a value.
  3. Final Answer:

    It has only a get accessor and no set accessor. -> Option B
  4. Quick Check:

    Read-only = get only [OK]
Hint: Read-only means only get accessor present [OK]
Common Mistakes:
  • Confusing read-only with write-only properties
  • Thinking both get and set are needed for read-only
  • Assuming no accessors means read-only
2.

Which of the following is the correct syntax for a write-only property named Password in C#?

public string Password { ? }
easy
A. set { _password = value; }
B. get { return _password; }
C. get; set;
D. private get; public set;

Solution

  1. Step 1: Identify write-only property syntax

    A write-only property has only a set accessor to allow writing but no reading.
  2. Step 2: Match syntax to write-only

    set { _password = value; } shows only a set accessor with assignment, which is correct for write-only.
  3. Final Answer:

    set { _password = value; } -> Option A
  4. Quick Check:

    Write-only = set only [OK]
Hint: Write-only means only set accessor present [OK]
Common Mistakes:
  • Using get accessor in write-only property
  • Using both get and set for write-only
  • Incorrect accessor visibility modifiers
3.

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);
medium
A. Compilation error
B. null
C. Runtime error
D. Alice

Solution

  1. Step 1: Analyze the property

    The Name property is read-only with a get accessor returning "Alice".
  2. Step 2: Check output of Console.WriteLine

    Calling user.Name returns "Alice", so it prints "Alice".
  3. Final Answer:

    Alice -> Option D
  4. Quick Check:

    Read-only property returns stored value [OK]
Hint: Read-only property returns stored value when accessed [OK]
Common Mistakes:
  • Expecting a compilation error due to missing set
  • Thinking write-only properties can be read
  • Confusing private field with property access
4.

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.

medium
A. The private field _pin should be public.
B. The set accessor should return a value.
C. The property has both get and set accessors, so it is not write-only.
D. The property should be static.

Solution

  1. Step 1: Check property accessors

    The property has both get and set accessors, allowing reading and writing.
  2. Step 2: Understand write-only requirement

    Write-only properties must have only a set accessor and no get accessor.
  3. Final Answer:

    The property has both get and set accessors, so it is not write-only. -> Option C
  4. Quick Check:

    Write-only = set only, no get [OK]
Hint: Write-only means no get accessor allowed [OK]
Common Mistakes:
  • Leaving both get and set for write-only
  • Changing field visibility instead of property
  • Expecting set to return a value
5.

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?

hard
A. public string Data { set { Console.WriteLine(value); _data = value; } }
B. public string Data { get { return _data; } private set { _data = value; } }
C. public string Data { get; set; }
D. public string Data { private get; set { Console.WriteLine(value); _data = value; } }

Solution

  1. Step 1: Identify write-only property with logging

    The property must have only a set accessor to be write-only and log the value when set.
  2. Step 2: Check each option

    public string Data { set { Console.WriteLine(value); _data = value; } } has only a set accessor that logs and assigns the value, matching requirements.
  3. Final Answer:

    public string Data { set { Console.WriteLine(value); _data = value; } } -> Option A
  4. Quick Check:

    Write-only with logging in set accessor [OK]
Hint: Write-only property has only set accessor with logging code [OK]
Common Mistakes:
  • Adding get accessor making property readable
  • Using private get instead of no get
  • Not logging inside set accessor