Back to course

Properties (Getters and Setters)

C# Zero to Hero: Comprehensive Programming Masterclass

Properties

Properties provide a flexible mechanism to read, write, or compute the value of a private field.

Automatic Properties

csharp class Person { public string Name { get; set; } }

Logic in Properties

csharp private string _secret; public string Secret { get { return _secret; } set { _secret = value; } }

This allows you to validate data before it is saved.