Understanding Properties in C#

Document

In a C# application, a Property might look like a simple variable for storing and retrieving values. However, a property is fundamentally different from a variable.

A property automatically generates a hidden field during the compilation phase, which is then accessed through getter and setter methods. Although a property appears to store data, it actually works by wrapping storage and retrieval within methods.

Example

public int Score { get; private set; }

At first glance, it looks like there is a simple variable named Score. But during compilation, it transforms into:

[CompilerGenerated]
private int <Score>k__BackingField;

public int Score
{
    [CompilerGenerated]
    get { return this.<Score>k__BackingField; }

    [CompilerGenerated]
    private set { this.<Score>k__BackingField = value; }
}
  • <Score>k__BackingField is the hidden field automatically created by the compiler for the Score property.
  • This field is protected so that users cannot directly access or modify it.

Thus, in C#, a property is a combination of a hidden field and get/set methods.

Why Use Properties?

  • To prevent direct external modification of variables.
  • To easily insert additional logic when getting or setting values.
  • To improve code consistency and data protection.

For these reasons, C# separates fields (variables) and properties.

Types of Properties : Auto-Implemented and Manual

When writing a property in C#, there are two approaches:
  • Auto-implemented properties
  • Manually implemented properties

Auto-Implemented Property

Auto-implemented properties don't require the developer to explicitly declare a variable. Simply defining the property is enough; the compiler creates a hidden backing field automatically.

public int Score { get; private set; }

With just this declaration, the compiler generates the hidden field, as well as the methods to store and retrieve the value. This keeps the code clean and concise, ideal for simple data storage without special processing.

Manual Property

A manual property requires the developer to explicitly declare a variable.

private int _score;

public int Score
{
    get { return _score; }
    private set
    {
        if (value < 0)
            _score = 0;
        else
            _score = value;
    }
}

Here, instead of a hidden backing field, the manually declared variable _score is used. Additional logic, such as validation or value adjustment, can be freely added inside the set block.

Manual properties are suitable when data needs validation, correction, or any special handling during assignment.

Summary

  • Auto-implemented properties eliminate the need to declare variables manually and automatically create hidden fields.
  • Manual properties allow developers to declare their own variables and insert custom logic during value assignment or retrieval.
  • For simple data storage, auto-implemented properties are convenient. For complex data handling, manual properties are more appropriate.

Popular posts from this blog

Understanding Arrays as Reference Types in C#

Setting Up a Basic Follow Camera with Cinemachine 3.x

Understanding and Using ? (nullable) in C#