Content Archived

This content is no longer current. Our recommendation for up to date content:

Working with Collections - 21

Play Working with Collections - 21

The Discussion

  • User profile image

    nice job, thank you 

  • User profile image
    Deactivated 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

  • User profile image

    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.

  • User profile image

    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!

  • User profile image
    Deactivated 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.




  • User profile image

    @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 and if you look at the various code samples you will see that they use both forms.

  • User profile image

    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

  • User profile image
    Deactivated User

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




  • User profile image

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

  • User profile image

    opening thread.

  • User profile image

    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.

  • User profile image

    @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.

  • User profile image

    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.



  • User profile image

    @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. 

  • User profile image
    Pete Ladd

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


  • User profile image

    @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.

Add Your 2 Cents