0
0
CsharpComparisonBeginner · 4 min read

Property vs Field in C#: Key Differences and Usage

In C#, a field is a variable declared directly in a class or struct to store data, while a property provides controlled access to that data through get and set accessors. Properties allow encapsulation and validation, whereas fields expose data directly.
⚖️

Quick Comparison

This table summarizes the main differences between fields and properties in C#.

AspectFieldProperty
DefinitionVariable inside a class/structMember with get/set accessors
Access ControlDirect access (public/private)Controlled via get/set methods
EncapsulationNo encapsulationSupports encapsulation and validation
SyntaxSimple declarationRequires get and/or set blocks
UsageStores data directlyManages access to data
Binding SupportNot supportedSupports data binding in UI frameworks
⚖️

Key Differences

Fields are simple variables declared inside a class or struct. They hold data directly and can be public or private. Public fields expose data openly, which can lead to unwanted changes from outside the class.

Properties act like smart fields. They use get and set accessors to control how data is read or changed. This allows you to add validation, logging, or other logic when data is accessed or modified. Properties help keep your data safe and your code flexible.

Unlike fields, properties can be used in data binding scenarios in UI frameworks like WPF or Xamarin, making them essential for modern C# applications. Also, properties can be virtual or abstract, supporting inheritance and polymorphism, while fields cannot.

💻

Field Example

This example shows a class with a public field storing a person's age.

csharp
public class Person
{
    public int age; // field
}

class Program
{
    static void Main()
    {
        Person p = new Person();
        p.age = 30;
        System.Console.WriteLine($"Age: {p.age}");
    }
}
Output
Age: 30
↔️

Property Equivalent

This example shows the same class using a property to control access to the age value.

csharp
public class Person
{
    private int _age; // private field

    public int Age  // property
    {
        get { return _age; }
        set
        {
            if (value >= 0) // validation
                _age = value;
        }
    }
}

class Program
{
    static void Main()
    {
        Person p = new Person();
        p.Age = 30;
        System.Console.WriteLine($"Age: {p.Age}");

        p.Age = -5; // ignored due to validation
        System.Console.WriteLine($"Age after invalid set: {p.Age}");
    }
}
Output
Age: 30 Age after invalid set: 30
🎯

When to Use Which

Choose fields for simple, internal data storage when you do not need to control access or add logic. Fields are best kept private to protect data integrity.

Choose properties when you want to control how data is accessed or modified, add validation, or support data binding. Properties provide flexibility and encapsulation, making your code safer and easier to maintain.

In modern C# development, prefer properties for public data exposure and keep fields private.

Key Takeaways

Fields are simple variables; properties provide controlled access with get/set.
Use properties to add validation and encapsulation around data.
Keep fields private and expose data via properties for safety.
Properties support features like data binding and inheritance.
Prefer properties for public data and fields for internal storage.