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?
| Feature | Field | Property |
|---|---|---|
| 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
Nameproperty (read-write) - A
RegistrationDateproperty (read-only) - A
PrivateNoteproperty (write-only)
Then instantiate it in a form, and use a Label to display the name and date (but not the note!).
π Summary
| Property Type | Syntax Example | Use Case |
|---|---|---|
| Read-Write | public int Age { get; set; } | Most common |
| Read-Only | public string Id { get; } | IDs, timestamps, results |
| Write-Only | public 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
setto 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.