locked
object and collection initializations analysis RRS feed

  • Question

  • This question relates to object and collection initializations. Please see this snippet
    snippet1

    //snippet1

    class Cat { // Auto-implemented properties. public int Age { get; set; } public string Name { get; set; } } Cat cat = new Cat { Age = 10, Name = "Fluffy" };snippet2snippet2

    //snippet2

    List<Cat> cats = new List<Cat>
    {
    new Cat(){ Name = "Sylvester", Age=8 },
    new Cat(){ Name = "Whiskers", Age=2 },
    new Cat(){ Name = "Sasha", Age=14 }
    };now if you notice the parenthesis part after Cat
    in snippet1  it is :  Cat{//.....etc};
    in snippet2 it is :  Cat() {//......etc}
    q1)  is there a logic that we used the () in snipppet 2
    q2)  is my approach to the subject seeking logic in every case, correct
    or should I just try to remember them?


    Saturday, February 15, 2014 6:45 AM

Answers

  • The parenthesis are optional. All you need is one initializer for an object: either the parentheses, which specify that you want to use a constructor, or the braces, which specify you want to set properties or array values. Here's the docs page on C# initializers: http://msdn.microsoft.com/en-us/library/bb384062.aspx.

    Wasabi Fan

    • Proposed as answer by Eyal Solnik Saturday, February 15, 2014 7:56 AM
    • Marked as answer by amigo 1 Saturday, February 15, 2014 8:08 AM
    Saturday, February 15, 2014 7:15 AM
  • They say : "There is more than one way to skin a cat".  It is especially is relative in this case where your class is a Cat.  The syntax is very similar for basic C#, collections, and Linq.  Using a parenthsis is standard C# syntax when calling a constructor and can be used in both cases.  When you don't use the parenthsis you are initializing an array which I think is using collection syntax so you would need to include the header using System.Collections.Generic.


    jdweng

    • Proposed as answer by Eyal Solnik Saturday, February 15, 2014 7:56 AM
    • Marked as answer by amigo 1 Saturday, February 15, 2014 8:08 AM
    Saturday, February 15, 2014 7:18 AM

All replies

  • The parenthesis are optional. All you need is one initializer for an object: either the parentheses, which specify that you want to use a constructor, or the braces, which specify you want to set properties or array values. Here's the docs page on C# initializers: http://msdn.microsoft.com/en-us/library/bb384062.aspx.

    Wasabi Fan

    • Proposed as answer by Eyal Solnik Saturday, February 15, 2014 7:56 AM
    • Marked as answer by amigo 1 Saturday, February 15, 2014 8:08 AM
    Saturday, February 15, 2014 7:15 AM
  • They say : "There is more than one way to skin a cat".  It is especially is relative in this case where your class is a Cat.  The syntax is very similar for basic C#, collections, and Linq.  Using a parenthsis is standard C# syntax when calling a constructor and can be used in both cases.  When you don't use the parenthsis you are initializing an array which I think is using collection syntax so you would need to include the header using System.Collections.Generic.


    jdweng

    • Proposed as answer by Eyal Solnik Saturday, February 15, 2014 7:56 AM
    • Marked as answer by amigo 1 Saturday, February 15, 2014 8:08 AM
    Saturday, February 15, 2014 7:18 AM
  • Hi

    this is in reply to both of you teachers

    is it nice to think each case with logic or i may remember it ,just that (mug-up)

    Saturday, February 15, 2014 7:36 AM