Re:The future of C# and Visual Basic [video]
by Permit on Monday November 03, 2014 @12:17PM
(#8551985)
>These become a lot more useful with primary constructors (also a new C# 6 feature):
Primary constructors have been removed: https://roslyn.codeplex.com/discussions/568820
In its simplest form, it means you can do this:
Your class now has a Foo property that nobody can set, not even methods in the class itself, but that can be publicly read.These become a lot more useful with primary constructors (also a new C# 6 feature):
This will create a constructor on the class that takes the usersFoo argument, and immediately assign the value of that argument to your read-only automatic property. It makes immutable classes a lot easier to create, and guarantees that they'll stay immutable.edit: Permit made it clear that primary contructors have been dropped from the C# 6 spec. You'll just have to assign your readonly properties from regular constructors then :)
Internally, .Net automatic properties are compiled with a backing field and a getter and setter method. In the case of a read-only auto property, they will omit the setter, so there is no public artifact that could be abused.
A bit messy, but an overview with code samples can be found at http://msdn.microsoft.com/en-us/magazine/dn683793.aspx