I was wondering, what is the preferred way to construct a new object in C#?
Take a Person class:
public class Person
{
private string name;
A few thoughts:
You need public properties to use Object Initializers. So if there's something you don't want to expose, you have to initialize them by constructor parameter.
If you check IL, you will find Object Initializer is not "atomic". If you write code like this (not that I recommend, just an example):
using (p = New Person() {Name = GetName(), Age = GetAge()})
{
//blah, blah
}
If there's an exception in GetAge(), you will create a instance of Person in a corrupted state. Worse, you can never enter the using scope and that instance will not be disposed as you would imagine.