What are Automatic Properties in C# and what is their purpose?

Automatic Properties are used when no additional logic is required in the property accessors. The declaration would look something like this: public int SomeProperty { get; set; } They are just syntactic sugar so you won’t need to write the following more lengthy code: private int _someField; public int SomeProperty { get { return _someField;} … Read more

C# Lazy Loaded Automatic Properties

No there is not. Auto-implemented properties only function to implement the most basic of properties: backing field with getter and setter. It doesn’t support this type of customization. However you can use the 4.0 Lazy<T> type to create this pattern private Lazy<string> _someVariable =new Lazy<string>(SomeClass.IOnlyWantToCallYouOnce); public string SomeVariable => _someVariable.Value; This code will lazily calculate … Read more

Public Fields versus Automatic Properties

In a related question I had some time ago, there was a link to a posting on Jeff’s blog, explaining some differences. Properties vs. Public Variables Reflection works differently on variables vs. properties, so if you rely on reflection, it’s easier to use all properties. You can’t databind against a variable. Changing a variable to … Read more