Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am trying to decode a piece of code from a book:

List<Person> people = new List<Person>()
{
new Person {FirstName="Homer",LastName="Simpson",Age=47},
new Person {FirstName="Marge",LastName="Simpson",Age=45}
};

Person is just a simple class they made, with a bunch of fields: Name, Last Name, etc...

What I don't understand is, don't we send parameters to a constructor of Person in non-curly brackets? I tried replicating this code, but it doesn't seem to fly, any takers?

Thanks for input.

share|improve this question
3  
    
"I tried replicating this code, but it doesn't seem to fly" <- you need to be using C# 3 (VS 2008) or later for this syntax to be available. –  AakashM Oct 29 '12 at 9:41
    
Be aware of the differences between the two concepts: Object Initialization and Object Population. In general you do initialization in the constructor. The method of your example is best used for population not for initialization. –  Emmad Kareem Oct 30 '12 at 5:05
add comment

1 Answer

up vote 16 down vote accepted

C# allows you to specify property parameters in curly braces when the object is initialized. This allows you to pick and choose which items to initialize and which to leave as defaults.

A constructor, on the other hand, runs one single block of code with a fixed set of parameters. So to get the same effect you'd have to create multiple constructors all with the various combinations of properties you might want to initialize, which could be tedious.

var x = new Person {FirstName="Homer",LastName="Simpson",Age=47}; 

is exactly equivalent to this:

var x = new Person();
x.FirstName="Homer";
x.LastName="Simpson";
x.Age=47;

Except that it's shorter and arguably easier on the eyes.

It also allows for constructs like you demonstrated in your question, which would be very tedious if you had to create temporary variables and initialize them out as I did here before adding them to the list. (Which is how you used to have to do it.) All without requiring an explicitly-defined constructor that takes your desired list of parameters, which may or may not be available.

Also, note that while a constructor can initialize properties with a private setter, this technique (as should be obvious from the provided example) will only work if you have a public setter for the property. Also note that my shortened example implicitly called the default (parameterless) constructor, which would therefore have to be present.

share|improve this answer
    
Might be worth adding that due to this, the properties initialized in the curly braces must be publicly settable. –  Yam Marcovic Oct 29 '12 at 5:38
    
@YamMarcovic ah. yes. Note added to that effect. –  tylerl Oct 29 '12 at 5:44
1  
Ah, I am enlightened, excellent explanation. –  RealityDysfunction Oct 29 '12 at 6:25
1  
To get a very similar effect with a single constructor, you could make all of its parameters optional. Then you could do something like new Person(lastName: "Simpson"), leaving out the firstName and age parameters. –  svick Oct 29 '12 at 8:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.