IS there a difference between those 2 ways of object creation?
new MyClass() { Id = 1, Code = \"Test\" };
or
MyClass c = ne
I tested by creating 100 million objects each using a parameterised constructor, a parameterless constructor with initialiser, and a parameterless constructor with setters, and there is no measurable difference at all. There was a slight difference in execution time, but running the tests in different order changed the results, so the differences are just due to the garbage collector kicking in at different times.
Creating 100 million objects took about 1.5 seconds, so there isn't much reason to try to make it faster either.
Personally I prefer a parameterised constructor, as I then can make the property setters private so that I can make the class immutable if I want to:
class MyClass {
public int Id { get; private set; }
public string Code { get; private set; }
public MyClass(int id, string code) {
Id = id;
Code = code;
}
}
Also, this way you can make sure that all properties have been set properly when the object is created.