This morning, I had a discussion with a co-worker regarding the following two example of initializing the code.
Example 1:
var employee = new Employee();
employee.FirstName = GetFirstName(); // this may get values from db
Example 2:
var employee = new Employee()
{
FirstName = GetFirstName()
}
My co-worker's argument was that example 2 is more efficient and if that object has any errors during initialization, it is automatically removed from heap. I am thinking that the compiled version of the code would look similar to example 1.
My question is that is my co-worker correct in his argument? When should you use property assignment instead of object initialization? Are there any best practices around this? MS doesn't have anything ...