Object and Collection Initializers - Day 2 - Part 7
- Posted: Nov 11, 2010 at 5:46 PM
- 22,727 Views
- 3 Comments
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Right click “Save as…”
Since C# 3.0, a new, shortened syntax used to create an instance of a new class and set its properties (initialization) all in one line of code became popularized. Entire collections can be created and initialized in this manner as well. Since many code examples on MSDN and the Internet at large use this new syntax, and because it is very convenient, Bob demonstrates how to dramatically shorten your code while explaining how to understand object and collection initializers.
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?
Great video, always good to use less lines of code. Less is more, as they say
The second shortened code example only does not show anything in the textblock when the button is clicked... the last code example works perfectly...
I tried to see what the error is but I didn't see anything wrong and this was no bugs so...
List<Car> myList = new List<Car>(); Car car1 = new Car() { Make = "Oldsmobile", Model = "Cutlas Supreme" }; Car car2 = new Car() { Make = "Geo", Model = "Prism" }; Car car3 = new Car() { Make = "Nissan", Model = "Altima" };@Xtianus:
this is because you still need to add the cars to the list:
myList.Add(car1);myList.Add(car2);myList.Add(car3);
Remove this comment
Remove this thread
close