ποΈ C# Properties β Encapsulate Fields with Clean Syntax
π§² Introduction β Why Use Properties in C#?
In object-oriented C# development, itβs essential to encapsulate class fields to protect data and provide controlled access. Properties allow you to do just thatβserving as a bridge between private fields and the outside world, using familiar method-like logic but with clean syntax.
π― In this guide, youβll learn:
- What properties are in C#
- Auto-implemented vs full properties
- Getters and setters explained
- Read-only and write-only properties
- Real-world use cases and best practices
π Core Concept β What Is a Property in C#?
A property in C# is a class member that provides a flexible mechanism to read, write, or compute the value of a private field.
It combines the benefits of encapsulation with easy access syntax like a field:
object.PropertyName = value;
Console.WriteLine(object.PropertyName);
π£ Full Property Syntax
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
π Use Case: Allows validation, logging, or transformation when getting/setting values.
π» Auto-Implemented Properties
public int Age { get; set; }
π Use Case: When no extra logic is needed for getting/settingβperfect for simple data containers.
β Read-Only and Write-Only Properties
π’ Read-Only Property:
public string ID { get; } = Guid.NewGuid().ToString();
π Use Case: Values assigned once and readable outside the class.
π Write-Only Property:
private string password;
public string Password
{
set { password = value; }
}
π Use Case: Set sensitive data without exposing it.
π Expression-Bodied Properties (C# 6+)
public int Square => number * number;
π Use Case: Computed properties with a single return expression.
π§± Backing Fields
private double _price;
public double Price
{
get { return _price; }
set
{
if (value >= 0)
_price = value;
}
}
π Use Case: Add validation logic to property setters.
π‘ Best Practices & Tips
π‘ Tip: Use auto-properties when no logic is needed.
β οΈ Pitfall: Avoid exposing fields directly. Always use properties to allow future changes without breaking the interface.
π Best Practice: Use PascalCase for property names and camelCase for backing fields.
π οΈ Real-World Use Cases
- π Form input validation (e.g.,
Age
,Email
) - π Controlling sensitive access (e.g., passwords, tokens)
- π¦ DTOs (Data Transfer Objects) for APIs
- π§ Business models (e.g.,
TotalPrice
,OrderStatus
)
π Summary β Recap & Next Steps
π§΅ Key Takeaways:
- Properties are encapsulated accessors for fields in C#.
- Use
get
,set
to control access. - Auto-properties are ideal for simple models; full properties allow validation and custom logic.
βοΈ Real-world relevance: C# properties are foundational in class design, data models, MVVM patterns, and entity frameworks.
β FAQ Section
β What’s the difference between a property and a field in C#?
β
A field is a direct data member; a property provides controlled access to that data.
β Can I have only a getter or only a setter?
β
Yes! Omit get
to make it write-only, or omit set
to make it read-only.
β When should I use a backing field?
β
Use a backing field when you need logic like validation or transformation inside your get
or set
methods.
β Are auto-properties backed by fields internally?
β
Yes. The compiler automatically creates a private backing field for auto-properties.
β Can properties be abstract or virtual?
β
Yes. Properties support inheritance, overriding, and interface implementation.
Share Now :