Working with Collections - 21
- Posted: Nov 21, 2011 at 9:21 AM
- 49,831 Views
- 11 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…”
Collections are a more powerful form of arrays. In this lesson we demonstrate an "old style" collection (pointing out its limitations) as well as several of the newer, strongly typed generic collections (List<T> and Dictionary<T1, T2>) utilizing the generics syntax.
Already have a Channel 9 account? Please sign in
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?
nice job, thank you
Hello Mr. Tabor,
First of all, thank you for the series. It's awesome and has helped me a lot.
There is one thing, though, that I don't understand in this video. Why is it that when you are creating a Car object you are using the sytnax
but when you are initializing the collection you are using
Hi Mihai,
I'll give it a go & try to explain, as I just watched the tutorial.
First they are 2 different processes, so therefore using different syntax... as defined by the C# designers.
The brackets are for declaring which "Constructor", to use when creating an instance. A constructor with () and no parameters usually means the default 'internal' constructor will be evoked. Go back to the first lesson on classes for more info.
The curly brackets are used to assigned data to the named fields, just an alternate way instead of in a constructor.
So
List<Car> myList = new List<Car>()
Will create a List of Cars called myList, again invoking the default 'internal' constructor. So there is no need for a constructor to be explicitly called for each Car element.
But the each car element in the list is assigned it's initial data values, instead of through a constructor.
I hope that helps till you get a reply from Bob.
And thanks Bob for sharing your knowledge, as I'm self taught through reading books, having some things explained has helped & the various tips & tricks.
I think I spoke too soon, in the next video the following is used!
List<Car> myCars = new List<Car>() {
new Car() { Make="BMW", Model="550i", Color=CarColor.Blue, StickerPrice=55000, Year=2009 },
....
};
So I'll leave it to Bob!
Hello Fred,
Thank you for your reply.
I was not referring to the curly braces, but the parantheses. What I'm saying is that in the beginning of the video the "car1" object is initialized with the following line:
As you said, we are using the paranthesis here since we are calling the default constructor. However, later in the video (21:00), when initializing the collection, Mr. Tabor is using the following line:
new Car { Make = "Oldsmobile", Model = "Cutlas Supreme" } ...
See the difference? There is no () here when creating the Car object. This is what I was talking about.
Regards,
Mihai
@MihaiMorariu: Mihai, this is just a syntax difference and is really personal preference. When you create an object and pass in a list of initial values, the parantheses can be skipped and they are assumed. So
Car car = new Car() {color = "red"};and
Car car = new Car {color = "red"};are functionally equivalent. Personally I use the first, because the second looks odd to me (and apparently to you as well
), but either will work. You can see this on MSDN at http://msdn.microsoft.com/en-us/library/bb384062.aspx and if you look at the various code samples you will see that they use both forms.
for those that are interested, the two lines produce the same IL (intermediate language, the code that C# is compiled into so that the .NET runtime can execute it)
Thank you for your help, Duncan ! It makes sense now.
Regards,
Mihai
@Duncanma:Wow, nice work there. Thanks for helping out. For the record, I may have temporarily lost my mind because I would prefer to use the (), too! Sorry for the confusion, but sometimes it's those mistakes that help you learn something (like I learned with your IL example!)
opening thread.
Hi Bob,
Thank you for this amazing lesson, it is of great help to me.Although i have a query
in line 73 of your code
Console.WriteLine(myDictionary["Geo"].Model);
i accidentally wrote the following
Console.WriteLine(myDictionary["Geo"]);
and got the following display on the console
"Collections.Car"
are we not supposed to get some kind of error. I did not understand how that output came to be. I understand that myDictionary["Geo"] points to a particular car type object in the dictionary.
Remove this comment
Remove this thread
close