difference between initializing an object with the

2019-01-23 23:50发布

This question already has an answer here:

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 ?

标签: c# class object
4条回答
霸刀☆藐视天下
2楼-- · 2019-01-24 00:00

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.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-24 00:07

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

查看更多
聊天终结者
4楼-- · 2019-01-24 00:12

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.

查看更多
劳资没心,怎么记你
5楼-- · 2019-01-24 00:18

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.

查看更多
登录 后发表回答