Why is it necessary to call :this() on a struct to use automatic properties in c#?

Note: as of C# 6, this isn’t required – but you should be using read-only automatically-implemented properties with C# 6 anyway…

this() makes sure that the fields are definitely assigned as far as the compiler is concerned – it sets all fields to their default values. You have to have a fully constructed struct before you can start accessing any properties.

It’s annoying, but that’s the way it is. Are you sure you really want this to be a struct though? And why use a protected setter on a struct (which can’t be derived from)?

Leave a Comment