Loading user information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading user information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Content Obsolete

This content is no longer current. We recommend you try the following url for more up to date content: http://channel9.msdn.com/Series/C-Fundamentals-for-Absolute-Beginners

Working with Collections - 21

24 minutes, 41 seconds


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.

Download the source code for Working with Collections


Follow the discussion

  • Oops, something didn't work.

    Getting subscription
    Subscribe to this conversation
  • Abdelhamida_derrar MSP

    nice job, thank you 

  • Deactivated UserDeactivated User

    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

    • Car car1 = new Car()   (with parantheses)

    but when you are initializing the collection you are using

    • new Car { Make = "Oldsmobile", Model = "Cutlas Supreme" }  (without parantheses)
    I hope my question does not sound stupid, but why exactly do we use parantheses in one case but do not in the other?
    Thank you,
    Mihai Morariu

  • fredfred

    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.


    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.

  • fredfred

    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!

  • Deactivated UserDeactivated User

    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:

    • Car car1 = new Car();

    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:

    • List<Car> myList = new List<Car> {

              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.




  • Duncan MackenzieDuncanma "yeah that's awful close, but that's not why I'm so hard done by"

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




                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 Smiley ), 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.

  • Duncan MackenzieDuncanma "yeah that's awful close, but that's not why I'm so hard done by"

    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)

      IL_0001:  newobj     instance void ConsoleApplication1.Car::.ctor()
      IL_0006:  stloc.2
      IL_0007:  ldloc.2
      IL_0008:  ldstr      "red"
      IL_000d:  stfld      string ConsoleApplication1.Car::color
      IL_0012:  ldloc.2
      IL_0013:  stloc.0
      IL_0014:  newobj     instance void ConsoleApplication1.Car::.ctor()
      IL_0019:  stloc.3
      IL_001a:  ldloc.3
      IL_001b:  ldstr      "red"
      IL_0020:  stfld      string ConsoleApplication1.Car::color
      IL_0025:  ldloc.3
      IL_0026:  stloc.1

  • Deactivated UserDeactivated User

    Thank you for your help, Duncan ! It makes sense now.




  • @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!)

  • Golnazgolnazal HEY HEY HEY

    opening thread.

  • VarunVarun

    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

    i accidentally wrote the following


    and got the following display on the console


    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.

  • @Varun: Funny enough, I did the same thing  Tongue Out When I realized what I had done, I of course added Model afterwards, but I wondering what the code to "retrieve all" form the Collection would have been, rather than just the "Model" in this case.

  • Hi Bob, Thanks a lot in making comfortable with Collections. Awesome work.

    Well, I was working with my own list of cars and wondering why can't list doesn't consider data integrity. Below is code bit.

    List<Car> myList = new List<Car>(){
    new Car() { Make="Toyota",Model="Camry"},
    new Car() { Make = "Nissan",Model="Sentra"},
    new Car() { Make = "Toyota",Model="Camry"}

    I thought code fails at runtime but to my wonder it went well. Is there any internal indexes are managing the data? Please let me know.



  • Clint RutkasClint I'm a "developer"

    @KPrasad: a List doesn't have to be unique items.  It isn't a key value paired, it is just a fancy array basically.

    You use different collection types for different use cases. 

  • Pete LaddPete Ladd

    Using Visual Studio 2012 Express. When I go to open either the before or after collections.sln file, nothing happens.


  • Clint RutkasClint I'm a "developer"

    @Pete Ladd: both programs from the ZIP are there to show you the concepts of collections.  The samples are tied to the video.  Looking at the code from a pure "I just open the solution and run the application" standpoint, they create an object or collection and then just call "Console.ReadLine();", that is it.

Remove this comment

Remove this thread


Comments closed

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.