Property (C#)

A property is a class member that looks like a field but uses get and set accessors to control reading and writing its value. Properties are the standard way to expose data in C#.

C#

An auto-implemented property like `public string Name { get; set; }` lets the compiler create the hidden backing storage for you. You can make a property read-only by leaving out set.

Properties let you add validation or computation later without changing how callers use them, which is why they are preferred over public fields.

Syntax

public Type Name { get; set; }

Examples

C#

class Account
{
    public decimal Balance { get; set; }
}

var a = new Account();
a.Balance = 100;
Console.WriteLine(a.Balance);

Output:

100

Learn it in a lesson

See also

Back to the Dictionary