25

generally i instant initialize an object when adding it to a list with this way --->

list.add(
   new foo() {                       //     <--- foo()
      field1 = value1,
      field2 = value2 
   }
);

but once i just tried --->

list.add(
   new foo {                     //     <--- foo
      field1 = value1,
      field2 = value2 
   }
);

& it worked !!!

in the 2nd way i am just creating an object without using () at tail of it. so does anyone have any idea about the difference between these various ways to initializing an object ?

Mankarse
  • 39,818
  • 11
  • 97
  • 141
Ashok Damani
  • 3,896
  • 4
  • 30
  • 48
  • 1
    There is an explanation on [Eric Lippert's blog](http://blogs.msdn.com/b/ericlippert/archive/2010/09/20/ambiguous-optional-parentheses.aspx) – StaWho Jun 14 '13 at 09:52

4 Answers4

29

so does anyone have any idea about the difference between these various ways to initializing an object ?

There's no difference at all. In both cases you're using an object initializer, and if you don't specify any constructor arguments, that's exactly equivalent to providing an empty list of constructor arguments. From section 7.6.10.1 of the C# spec:

An object creation expression can omit the constructor argument list and enclosing parentheses provided it includes an object initializer or collection initializer. Omitting the constructor argument list and enclosing parentheses is equivalent to specifying an empty argument list.

Note that when you just invoke a constructor without using an object initializer (the braces) you have to specify the constructor arguments. So:

Foo foo = new Foo();    // Valid
Foo foo = new Foo;      // Invalid
Foo foo = new Foo() {}; // Valid
Foo foo = new Foo {};   // Valid

The "valid" lines are all exactly equivalent, including any use of default parameters - so Foo might only have a constructor like this:

// You can invoke this without specifying arguments
public Foo(int x = 0)
{
}

See section 7.6.10 of the C# 5 spec for more details.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • but what if Foo doesn't have any constructor like u are saying `Foo(int x = 0)`. bcoz my code is still working without having a constructor – Ashok Damani Jun 14 '13 at 10:04
  • @AshokDamani: The default value will be applied. It's just syntactic sugar as though you wrote empty parenthesis. With the `x` parameter being an optional parameter, you _could_ call `var f = new Foo()` (which passes in `0`) or `var f = new Foo(){}` (which passes in `0`) or `var f = new Foo { }` (which passes in `0`) or you can explicitly specify it: `var f = new Foo(9999) { }` EDIT: Yeah, sorry about the `struct` question, they specify default constructors always; I overlooked that you declared `x` as an optional parameter. – Chris Sinclair Jun 14 '13 at 10:15
  • @AshokDamani: If you don't supply *any* constructors, then the C# compiler will supply a parameterless one for you, commonly known as a "default constructor". – Jon Skeet Jun 14 '13 at 10:19
9

They are both object initializers. There is no difference. It's a good question though. I would think that behind the scenes the result is the same.... Compiler creates object with empty constructor and sets properties.

ek_ny
  • 10,153
  • 6
  • 47
  • 60
6

No difference whatsoever. In an object initializer of the second form (no parentheses) the parameterless constructor is used by default, but can be specified explicitly. If you want to pass parameters to the constructor you must use the first form (with some values, obviously).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
3

As Explained mostly, no difference in both. Whenever we create object always there is default constructor created without any parameter, which initialize members of object with default values(empty/NULL), means your second method will be used. But when we pass parameter at the time of object initialization the constructor must be defined for the the matching definition else it will result in syntax error. If there is no constructor defined for matching definition then there is nothing to pass as values or to initialize object members.

Mihir Patel
  • 53
  • 1
  • 6