I was wondering what people thought of using properties as object initializers in C#. For some reason it seems to break the fundamentals of what constructors are used for.
An example…
public class Person { string firstName; string lastName; public string FirstName { get { return firstName; } set { firstName = value; } } public string LastName { get { return lastName; } set { lastName= value; } } }
Then doing object intialization with…..
Person p = new Person{ FirstName = 'Joe', LastName = 'Smith' }; Person p = new Person{ FirstName = 'Joe' };
What you see here is some syntatic sugar provided by the compiler. Under the hood what it really does is something like:
Person p = new Person( FirstName = ‘Joe’, LastName = ‘Smith’ );
So IMHO you are not really breaking any constructor fundamentals but using a nice language artifact in order to ease readability and maintainability.