5

Sometimes an initializer list is specified after the class name without using the () operator:

Cat cat = new Cat { Age = 10, Name = "Fluffy" }

Other times it is specified after the () operator:

List<Cat> cats = new List<Cat>
    {
        new Cat(){ Name = "Sylvester", Age=8 }
    }

I am assuming the difference is because here new Cat() is inside the list. But I still don't understand why it should be different. So why the difference, and when to use which syntax?

Thanks.

1

3 Answers 3

6

When you use the initializer list you can omit the (), when using a parameterless constructor. It does not matter with the new Cat() is inside the list or not.

6

You must specify the () when there is no default (parameterless) constructor - when you have to supply parameters.

When a class has default constructor (or a parameterless one), you can always omit the () when using an initializer. The compiler does the magic for you and you can think of things as - the compiler inserts them for you.

6
  • If there is no default constructor () wouldn't work no? The example I gave above is from MSDN, and there the Cat class doesn't have any constructors, meaning there is a default one. Jan 20, 2013 at 10:12
  • This answer is wrong as it is currently. You can not always omit the (). "new Cat;" would not work. You can omit it when you use the initializer list. Jan 20, 2013 at 10:13
  • @YechielLabunskiy - No, it wouldn't work. The default constructor is the one generated when you don't specify a constructor and it is a parameterless one.
    – Oded
    Jan 20, 2013 at 10:13
  • 1
    @user1908061 - In the context of the question (object initializers), you can always omit it.
    – Oded
    Jan 20, 2013 at 10:15
  • @Oded So you are saying there is no specific reason Cat() is called inside the list in my example but w/o () when outside the list? Meaning - both ways are acceptable here since Cat has no constructor defined (meaning there is a default one defined by the compiler) Jan 20, 2013 at 10:19
0

There is no real difference to which of the two you decide to use. Compare these two Cat instances:

var catA = new Cat();   // traditional constructor
catA.Name = "Mittens";  // set property

var catB = new Cat { Name = "Not Mittens" }; // all in one initializer

They are identical in functionality, just a shortcut notation. The first way is the traditional way, which most object-oriented (OO) languages create objects using the constructor. C# recently has added this new method of creating objects and setting properties to make code cleaner.

Even if your class does not have a parameterless constructor (new()), you can use the {...} initialization still.

Consider this:

var dogA = new Dog("Fido");
var dogA.Age = 12;

var dogB = new Dog("Not Fido") { Age = 7 };

Even if parameters are required in the constructor, you can still use this style of initialization as long as you supply them.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.