8️⃣ C# Object-Oriented Programming (OOP)
Estimated reading: 3 minutes 28 views

πŸ—οΈ 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ—οΈ C# Properties

Or Copy Link

CONTENTS
Scroll to Top