C# Interview Question #27

What is the difference between a field and a property?

Classes, Constructors & Properties Junior Beginner

Quick Interview Answer

A field is a variable declared directly inside a class or struct. A property is a member that exposes access through get, set, or init accessors.

Detailed Explanation

A field is a variable declared directly inside a class or struct. A property is a member that exposes access through get, set, or init accessors.

Fields are commonly kept private and used for internal state. Public properties are normally used to expose state to consumers because they support encapsulation, validation, computed values, and different accessor visibility.

For example, a property may be publicly readable while allowing modification only inside the class.

Code Example

private decimal _price;

public decimal Price
{
    get => _price;
    private set => _price = value;
}