C# Automatic Properties - Why Do I Have To Write "get; set;"?

Ben Aston picture Ben Aston · Dec 4, 2008 · Viewed 40.8k times · Source

If both get and set are compulsory in C# automatic properties, why do I have to bother specifying "get; set;" at all?

Answer

Brian Genisio picture Brian Genisio · Dec 4, 2008

Because you might want a read-only property:

public int Foo { get; private set; }

Or Write-only property:

public int Foo { private get; set; }