C# 3.0 introduced a very compact way of initializing objects of a class.
Previously we used to initialize objects like this:
MailMessage mailMessage = new MailMessage(); mailMessage.Subject=UIConstants.UIErrorSubject; mailMessage.Body = message; mailMessage.BodyEncoding = Encoding.GetEncoding(UIConstants.NewAccountMailBodyEncoding); mailMessage.From = new MailAddress(UIConstants.Me2AdminEmail);
Now, with the new feature known as Anonymous Constructors or Object Intializers we can do the same code in C# 3.0 and above like this:
MailMessage mailMessage = new MailMessage() { Subject = UIConstants.UIErrorSubject, Body = message, BodyEncoding = Encoding.GetEncoding(UIConstants.NewAccountMailBodyEncoding), From = new MailAddress(UIConstants.Me2AdminEmail) };
In the C# 3.0 code, there is no constructor that corresponds to the way I instantiated the object. This prevents developers from having to create a different constructor for each different set of properties that need to be set. It also make the code easier to read.