Why do automatic properties require both getters AND setters?

In C#, if I declare an auto-implemented property, why do I have to declare BOTH the get and set part?

i.e.

public string ThisWorks { get; set; }

public string ThisDoesnt { get; }

Isn't this just syntactic sugar - i.e. the compiler inserts a private field for the property? So why the problem?

Curious.

Answers


If you didn't have a setter - then how would you ever set the property?

Incidentally, you can specify the accessibility, eg:

public string Foo
{
  get;
  private set;
}

Need Your Help

Passing a parameter through server.execute?

asp-classic parameter-passing

It is possible to pass a parameter through server.execute?

Jquery check list of elements for a match

javascript jquery

I have some checkboxes grouped using the attribute rel. When anyone is unchecked, I want to check if all are also unchecked, then do something.