What do you think of using properties as object initializers in C#;

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" };

Answers


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" );

Person _p$1 = new Person();
_p$1.FirstName = "Joe";
_p$1.LastName = "Smith";
Person p = _p$1;

So IMHO you are not really breaking any constructor fundamentals but using a nice language artifact in order to ease readability and maintainability.


Need Your Help

Restaurant review site in SharePoint

sharepoint sharepoint-2007

I'm totally new to SharePoint but my company has adopted it for internal use. As a learning exercise I thought it would be fun to create a local restaurant review site. Can anyone point me to an ex...