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;
I always work on the basis that you should pass values to a constructor which are mandatory for that object to exist in a valid state.
In you example you could say that new person cannot exists without an age so that should be passed in to the contructor.
If you work on the basis that an object should model a real work entity then that determines the minimum necessary to make any object valid - whether you set those as default values or by passing values in via the constructor.
In my opinion, you should first decide what makes a person a person. What are the person's properties for a person object to be correctly instantiated. What are the minimum requirements for a person.
There should always be a constructor with the minimum requirements.
I would only use object initializers for types that don't require any properties to be instantiated. Hence the default constructor.
I know that object initializers are very convenient. Other mechanisms also might require an empty constructor in the object.
But I don't think that it makes a lot of sense that you could create a Person without a name.
The real issue comes when you pass the object through tiers/layers. For example you create a person object and pass through a webservice. The webservice at somepoint tries to deserialize and tries to instantiate and then you might get an error if there is a paramter required for constructors! as webservices first creates an object and then assigns values.
So I would prefer a parameterless contructor if it is a datamodel (POCO sort of) that needs to pass through tiers.
For other reasons contructor parameter is the best way(for mandatory fields). Especially when providing the class as an iterface to external objects or assemblies.
The preferred way depends on your design.
Constructor properties are for items that your object requires in order to be correctly constructed. That is to say, any properties the object should have in order to be initialized need to be in the constructor (you don't usually want a partially intialized object after the constructor is called unless you're creating a factory or builder pattern and the constructor is hidden from all but the factory/builder).
Property intializers are best for additional configuration after a constructor that is required by your particular use case but is not required for the object to be considered initialised.
For example, you could have an object that represents a person. A person needs a name and an age to be initialised, but the address they live at is an optional configuration. So, the name and age are constructor parameters, and the address is a read/write property.
Person johnDoe = new Person("John Doe", 24) { Address = "42 Adams Street" };
It really depends on the scenario. The property approach has a lot of convenience, in that you don't have to duplicate the assignments in the constructor. Furthermore, most data-binding scenarios like to be able to create new objects, for which they usually use the parameterless constructor, so that is a big advantage.
However, for immutable types, the constructor approach is the only sensible option. Interestingly (perhaps) the named/optional parameters in C# 4.0 allow something similar to object initalizers for immutable types - see here.
The constructor approach is also very popular for Inversion of Control frameworks, as it clearly advertises what that class needs in order to function.
You may need to mix and match, but usually more property-style than constructor-style.
My main considerations on this question are 1) How much data will the instantiating entity have when the object is instantiated and 2) How encapsulated does the class need to be? If, once set, the properties shouldn't be able to change (at least, due to any outside entities) then I would use the constructor, since the constructor can set any read-only properties as well as any read/write properties.
Also remember that, just like any other method, your Constructor can be overloaded, so you can set up any number of ways to initialize those properties.
In general, I use constructors, but there are times when I set the properties manually instead. Use the right tool for the right problem.