Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 631
Ben Aston Avatar asked Dec 04 '08 13:12

Ben Aston


1 Answers

Because you might want a read-only property:

public int Foo { get; private set; } 

Or Write-only property:

public int Foo { private get; set; } 
like image 142
Brian Genisio Avatar answered Sep 28 '22 16:09

Brian Genisio