0

Possible Duplicate:
Why are C# 3.0 object initializer constructor parentheses optional?

Assume this is my custom object:

public class MyObject {
    public string Name { get; set; }
    public int number { get; set; }

    public MyObject() {

    }
}

and this object can defined generic, or with custom constructors and a lot of more elements, so we can make an instance and set properties in two syntax

MyObject newObj1 = new MyObject { Name = "MyName", number = 10 };
MyObject newObj2 = new MyObject() { Name = "MyName", number = 10 };

Is there any differences between instance that use parentheses and another one without parentheses?

Community
  • 1
  • 1
Saeid
  • 13,224
  • 32
  • 107
  • 173

2 Answers2

2

Is there any differences between instance that use parentheses and another one without parentheses?

No.

If you use ReSharper it will will flag the second construct as redundant parenthesis and will suggest that you remove them. However, it is only a matter of style.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
1

Is there any differences between instance that use parentheses and another one without parentheses?

Yes,

The first is an implicit call to the default constructor, the second is an explicit call.

While functionally identical, the compiler sees them differently.