Home
Source: own resources, Authors: Agnieszka and Michał Komorowscy
I can bet that you've already heard about and used Auto-Property Initializers and that you love them. At least I do so ;) Here is a small example. In this case an Auto-Property Initializer was used to generate unique identifiers for instances of
Entity class. Trivial, isn't it?
public class Entity
{
public string Guid { get; } = System.Guid.NewGuid().ToString();
/*...*/
}
What is important we have guaranteed that a given initializer will be executed only
ONCE for an instance of a class. Otherwise it will have no sense! In other words our expectations is that if we create a new instance of
Entity class it's identifier will not change. It is generally true, but there are some caveats.