var album = new Album {Title= "Album"+ id }

Jan 18, 2013 at 5:12 PM

var album = new Album {Title= "Album"+ id } works 

instead of 

var album = new Album(Title="Album" +id) 

May I know how the constructor initializes the instance variable in the first case ?

Jan 21, 2013 at 8:02 PM
Edited Jan 21, 2013 at 8:03 PM

I try to explain in my own words: :(

 

It is like in creating an css file when you have to use the { } instead of ( ) because it won't be recognised or read.

 

I hope this is the answer you're looking for and hope that I understood you're question. :))

Coordinator
Jan 21, 2013 at 10:51 PM

This is taking advantage of C# Object Initializer syntax. From MSDN: "Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor."

More info here: http://msdn.microsoft.com/en-us/library/bb384062.aspx