Back to course

Class Members: Fields and Properties

.NET Zero to Hero: Master C# and Modern App Development

Fields vs Properties

Fields are variables inside a class. Properties are used to provide a flexible mechanism to read, write, or compute the value of a private field.

Why use Properties?

To control access (Encapsulation).

csharp class Person { private string name; // field

public string Name { // property
    get { return name; }
    set { name = value; }
}

}

Automatic Properties:

csharp public string Color { get; set; }