Properties and Access Modifiers
35 minProperties provide controlled access to class fields with get and set accessors, enabling validation, computation, and encapsulation. Properties look like fields but behave like methods. Getters retrieve values. Setters assign values with optional validation. Understanding properties enables proper encapsulation. Properties are preferred over public fields.
Auto-implemented properties automatically create backing fields, reducing boilerplate code. Auto-properties (public int Age { get; set; }) automatically create a private backing field. You can add access modifiers to getters or setters (public int Age { get; private set; }). Understanding auto-properties enables concise code. Auto-properties are common in modern C#.
Access modifiers (public, private, protected, internal) control visibility and accessibility of class members. public is accessible everywhere. private is accessible only within the class. protected is accessible in derived classes. internal is accessible within the same assembly. Understanding access modifiers enables proper encapsulation. Access modifiers are essential for security and design.
Read-only properties can only be set in constructors or init-only setters (C# 9.0+), preventing modification after initialization. Read-only properties use { get; } syntax. Init-only setters use { get; init; } and can be set during object initialization. Understanding read-only properties enables immutable objects. Read-only properties are useful for immutable data.
Properties can have different access levels for getters and setters (e.g., public getter, private setter), enabling read-only access from outside while allowing internal modification. This pattern is common for properties that should be set only internally. Understanding property access levels enables fine-grained control. Different access levels provide flexibility.
Best practices include using properties instead of public fields, using auto-properties when no validation is needed, using appropriate access modifiers, and making properties read-only when appropriate. Understanding properties and access modifiers enables proper encapsulation. Properties are fundamental to C# class design.
Key Concepts
- Properties provide controlled access with get and set accessors.
- Auto-implemented properties automatically create backing fields.
- Access modifiers (public, private, protected, internal) control visibility.
- Read-only properties can only be set in constructors or init-only setters.
- Properties can have different access levels for getters and setters.
Learning Objectives
Master
- Creating properties with getters and setters
- Using auto-implemented properties
- Applying access modifiers correctly
- Creating read-only properties
Develop
- Understanding encapsulation principles
- Designing secure class interfaces
- Appreciating properties' role in OOP
Tips
- Use properties instead of public fields for encapsulation.
- Use auto-properties when no validation or computation is needed.
- Use private setters for properties that should be read-only externally.
- Use init-only setters for immutable objects (C# 9.0+).
Common Pitfalls
- Using public fields instead of properties, breaking encapsulation.
- Not using access modifiers, exposing internal implementation.
- Creating properties that do too much, violating single responsibility.
- Not understanding property access levels, causing compilation errors.
Summary
- Properties provide controlled access to class fields.
- Auto-implemented properties reduce boilerplate code.
- Access modifiers control member visibility.
- Read-only properties enable immutable objects.
- Understanding properties enables proper encapsulation.
Exercise
Create a class with different types of properties and access modifiers.
using System;
public class Person
{
// Private field
private string _name;
// Auto-implemented property
public int Age { get; set; }
// Property with custom getter/setter
public string Name
{
get { return _name; }
set
{
if (!string.IsNullOrEmpty(value))
_name = value;
}
}
// Read-only property
public string FullName { get; }
// Property with expression body
public bool IsAdult => Age >= 18;
public Person(string name, int age)
{
Name = name;
Age = age;
FullName = name; // Can only be set in constructor
}
}
class Program
{
static void Main(string[] args)
{
var person = new Person("John Doe", 25);
Console.WriteLine($"Name: {person.Name}");
Console.WriteLine($"Age: {person.Age}");
Console.WriteLine($"Is Adult: {person.IsAdult}");
// This would cause a compilation error:
// person.FullName = "New Name";
}
}