C# > Object-Oriented Programming (OOP) > Encapsulation > Auto-Implemented Properties
Auto-Implemented Property with Private Set
This example demonstrates how to use auto-implemented properties with a private setter, allowing external access to read the property's value but restricting write access to within the class. This enhances encapsulation and data integrity.
Auto-Implemented Property with Private Set Example
This code defines a `Counter` class with an auto-implemented property `Count` of type `int`. The `get; private set;` syntax allows the `Count` property to be read from anywhere but can only be set from within the `Counter` class. The constructor initializes `Count` to 0, and the `Increment` method increases its value. The `Example` class demonstrates how to create a `Counter` object, increment the count, and access its value. The commented-out line shows that you cannot directly set `Count` from outside the class.
public class Counter
{
public int Count { get; private set; }
public Counter()
{
Count = 0;
}
public void Increment()
{
Count++;
}
}
public class Example
{
public static void Main(string[] args)
{
Counter counter = new Counter();
counter.Increment();
Console.WriteLine("Count: " + counter.Count);
// This line would cause a compile-time error because the setter is private:
// counter.Count = 10;
}
}
Concepts Behind the Snippet
Using a private setter (private set;
) in an auto-implemented property provides a way to create properties that are read-only from outside the class but can be modified internally. This is a key aspect of encapsulation, as it controls how data can be changed and prevents unintended modifications from external code.
Real-Life Use Case
This pattern is often used when you want to expose a property that represents the state of an object but only allow the object itself to modify that state. Examples include counters, identifiers generated by the system (e.g., `OrderId` which is generated when an order is created), or properties that are calculated based on other internal data.
Best Practices
init;
instead of private set;
in newer C# versions if the property should only be set during object initialization.
Interview Tip
Understand the difference between `get; set;`, `get; private set;`, and `get; init;`. Be prepared to discuss scenarios where each would be appropriate.
When to Use Them
Use auto-implemented properties with private setters when:
Memory Footprint
The memory footprint is the same as a regular auto-implemented property. The private set;
only restricts access, it doesn't affect memory allocation.
Alternatives
The alternative is to use a full property with a private backing field and a public getter. However, using get; private set;
is more concise and readable.
Pros
Cons
init;
instead of private set;
(if the initialization scenario fits).
FAQ
-
Can I use a protected setter instead of a private setter?
Yes, you can use a protected setter (protected set;
) if you want to allow derived classes to modify the property but still prevent external code from doing so. -
What is the difference between `private set;` and `init;`?
`private set;` allows the property to be set only from within the class at any time. `init;` allows the property to be set only during object initialization (either in the constructor or using object initializer syntax). `init;` implies immutability after initialization. -
If a property has a `private set;`, can I set the property in the constructor?
Yes, you can set the property in the constructor because the constructor is part of the class's internal implementation.