In our last session, we explored how access modifiers are the primary tool for implementing encapsulation, allowing you to hide a class's internal state. This led to a key question: if we make our data fields private, how do we provide safe, controlled access to them? This lesson provides the C#-native answer: properties.
Today, we will build directly on the principle of encapsulation by learning how to define and use properties. You will discover how they serve as a more elegant and powerful alternative to the traditional getter and setter methods you may have used in other languages. We'll cover the full syntax, from explicit backing fields to the concise and widely used auto-implemented properties. Mastering this is crucial for writing idiomatic C# code and for demonstrating your grasp of OOP fundamentals in your upcoming interview.
1. The Problem: The Fragility of Public Fields
In the previous lesson, we discussed why simply making a field public is a bad practice. It breaks encapsulation, allows for invalid data to be set, and creates a rigid contract with any code that uses your class. Any internal change to how that data is stored can cause a cascade of breaking changes across the application.
To see a practical and memorable demonstration of this exact problem, watch the following short video. It uses a scenario where a requirement change for a public field leads to significant code maintenance issues.
What is Encapsulation? - C# Gotcha! Interview Questions
This video from Ryan McBeth's "C# Gotcha!" series clearly illustrates why public fields are problematic and introduces properties as the solution.
Watch from the start of the main example up to the point where the refactored solution is complete. Pay attention to how a simple data type change breaks the consumer's code with a public field, but not with a property. This covers the core demonstration.
As the video demonstrates, by hiding the internal field and exposing it through a property, you gain the flexibility to change the internal implementation (e.g., from an int to a decimal) without breaking the code that consumes your class. The property acts as an intermediary, a public gateway to private data.
2. Fields vs. Properties: The Classic vs. C# Approach
Before C# had properties, and in many other object-oriented languages like Java, the standard way to provide controlled access to a private field was through explicit getter and setter methods.
- A field is a variable that holds data directly within a class instance.
- Getter/Setter methods are public methods whose sole purpose is to retrieve (
get) or update (set) the value of a private field.
Let's clarify this distinction with a reading that first shows the classic method-based approach and then introduces the C# property syntax as a superior alternative.
Difference Between “Field” and “Property” in C# OOP - Medium
This article clearly defines fields and properties, showing how properties streamline the process of creating getters and setters.
Start by reading the section defining fields and their use. Notice the GetName() and SetName() methods in the PersonTitleName class; this is the classic "getter/setter method" pattern. Next, read the section So, What is a Property? down to the first code block for the Person class. This introduces the C# property syntax with get and set accessors that wrap a private field.
The key takeaway is that a C# property provides a cleaner syntax for what you would otherwise write as two separate methods. Instead of calling person.GetName() and person.SetName("John"), you can use the more natural assignment syntax: string name = person.Name; and person.Name = "John";.
3. Implementing Properties with Validation
Properties are not just syntactic sugar; they are special methods that can contain logic. This is where their true power for encapsulation lies. You can add validation or conversion logic inside the set accessor to ensure the integrity of your object's state.
The following video provides a very direct, code-focused walkthrough of converting a private field into a public property with validation logic.
This video from Bro Code demonstrates how to create a property with get/set accessors and add validation logic to the setter.
Watch the segment from creating the property. The key part to focus on is how the set accessor uses the implicit value keyword and contains an if statement to enforce a rule on the data being assigned.
As you saw, the set block gives you a place to intercept incoming data (via the value keyword) and apply logic before it's assigned to the private backing field.
A common mistake when you're first learning this syntax is to refer to the property itself inside its own get or set accessor, instead of referring to the private backing field. This creates an infinite recursive loop, resulting in a StackOverflowException.
Difference Between “Field” and “Property” in C# OOP - Medium
This section highlights a frequent error when implementing properties.
Read the section under the Warning heading. Understanding this common pitfall is great for interviews, as it shows you grasp the underlying mechanism.
4. Auto-Implemented Properties: The Concise Syntax
In many situations, you don't need any special validation logic in your property accessors. You simply want to expose a private field. For this common case, C# provides a highly concise syntax called auto-implemented properties (or auto-properties).
When you use this syntax, the C# compiler automatically generates a hidden, private backing field for you.
public class Team
{
// This is an auto-implemented property.
// The compiler creates a private string field behind the scenes.
public string Name { get; set; }
}
This is the most common way you will see properties declared in modern C# code for simple data transfer objects (DTOs) and models.

The official Microsoft documentation provides a complete overview of this feature.
Automatically Implemented Properties - C# | Microsoft Learn
This document is the definitive guide to auto-properties.
Read the first few paragraphs and the first code example. This explains the core concept and syntax. Then, focus on the section discussing immutability. Read about the three options for making objects immutable: get-only accessor get and init accessors private set accessor These techniques are essential for creating robust and thread-safe data objects.
The init keyword is particularly useful. It allows a property to be set only during object initialization (e.g., var team = new Team { Name = "Warriors" };), but not changed afterward (team.Name = "Lakers"; would be a compile error). This helps you create truly immutable objects, a practice highly valued in modern software engineering.
Conclusion
You now have a solid understanding of C#'s property system, a cornerstone of its approach to object-oriented programming. This feature provides a robust and elegant syntax for encapsulation that is more powerful and readable than the traditional getter/setter methods found in other languages.
Your key takeaways are:
- Fields vs. Properties: Fields are for internal data storage. Properties are public-facing members that provide controlled access to that data.
- Full Property Syntax: A property can be explicitly backed by a private field, allowing for custom logic (like validation) in the
getandsetaccessors. - Auto-Implemented Properties: For simple cases with no custom logic, use the
{ get; set; }syntax. The compiler handles creating the backing field for you. This is the modern, idiomatic C# approach. - Controlling Mutability: You can control how a property is set by using
get-only,init, orprivate setaccessors to create more robust, immutable objects.
In our next lesson, we will expand from defining the members of a single class to exploring how classes can relate to one another. We will cover inheritance, method overriding, and method overloading, fundamental concepts that allow you to create hierarchies of related objects and promote code reuse.