Read-only, Write-only, and Read-Write Properties in C#

Control how data is accessed and changed in your WinForms apps

Properties in C# let you safely expose data from a class without giving away total control. By defining properties as read-only, write-only, or read-write, you decide what outside code can see or change — ideal for protecting logic in WinForms applications.

Let’s explore each type and how to use them effectively.


🧱 What Are Properties?

Properties are like smart fields — you can get/set values but add logic, validation, or restrictions behind the scenes.

public string Name { get; set; }  // A simple read-write property

🔓 Read-Write Properties

These are the most common: both get and set are defined, so values can be read and changed.

public int Age { get; set; }

Age = 25;               // Write
int myAge = Age;        // Read

✅ Use when outside code needs full access


🔐 Read-Only Properties

Only get is provided — outside code can see the value but not change it.

public string Id { get; }

public MyUser()
{
    Id = Guid.NewGuid().ToString();  // Set internally
}

✅ Good for calculated values, timestamps, IDs, and results
✅ Ensures that once set, it can’t be overwritten


✍️ Write-Only Properties

Only set is provided — outside code can set the value but not read it.

private string password;

public string Password
{
    set { password = Hash(value); }
}

✅ Useful for sensitive data (like passwords)
✅ Prevents accidental display or logging of private info


🧪 Example: Create a User Class

public class User
{
    public string Name { get; set; }             // Read/Write
    public DateTime CreatedAt { get; }           // Read-Only
    private string _password;

    public string Password                      // Write-Only
    {
        set { _password = value; }
    }

    public User(string name)
    {
        Name = name;
        CreatedAt = DateTime.Now;
    }
}

Usage:

User user = new User("Alex");
user.Password = "secret123";     // ✅ Can write
string userName = user.Name;     // ✅ Can read
// string p = user.Password;     // ❌ Compile error – write-only

🧠 Why Use Properties Instead of Public Fields?

FeatureFieldProperty
Direct data access✅ Yes❌ Controlled
Validation logic❌ No✅ Yes
Binding support (WinForms, WPF)❌ No✅ Yes
Can be read-only❌ No✅ Yes

✅ Properties offer data protection, flexibility, and future-proofing


✨ Extra: Expression-Bodied Properties

Short-hand for simple getters:

public int Age => DateTime.Now.Year - BirthYear;

✅ Cleaner syntax for read-only calculated values


🧪 Quick Challenge

🧩 Create a Student class with:

  • A Name property (read-write)
  • A RegistrationDate property (read-only)
  • A PrivateNote property (write-only)

Then instantiate it in a form, and use a Label to display the name and date (but not the note!).


📚 Summary

Property TypeSyntax ExampleUse Case
Read-Writepublic int Age { get; set; }Most common
Read-Onlypublic string Id { get; }IDs, timestamps, results
Write-Onlypublic string Password { set { ... } }Private/sensitive data

✅ Best Practices

  • ✅ Use read-only for data that should never change externally
  • ✅ Use write-only for things like passwords or logs
  • ✅ Use read-write only when it makes sense
  • ✅ Add validation in set to protect data integrity
  • ✅ Back properties with private fields for control

🎓 Want to Go Further?

  • Learn auto-properties with default values
  • Use properties in data-binding (e.g., with DataGridView)
  • Explore INotifyPropertyChanged for UI updates in MVVM

💬 Want help writing a class for your form using properties?
We’ll help you model the data the clean, safe way.