-6

What is the difference between newing up a class with and without parenthesis?

Class instance = new Class()
{
    X = 1,
    Y = 2,
    Z = 3
};

vs.

Class instance = new Class
{
    X = 1,
    Y = 2,
    Z = 3
}
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
  • 2
    That's not Java. Please ask a specific and clear question. And you're asking about language syntax -- how can that be "language-agnostic"? – Hovercraft Full Of Eels Jun 04 '16 at 14:22
  • @HovercraftFullOfEels - notice I also tagged language-agnostic and C#? – Matthew Layton Jun 04 '16 at 14:23
  • 1
    You've tagged the question as language-agnostic, and then asked a question which is about relevant to syntax of a language. This question is impossible to answer without restricting to a specific language. – Harry Harrison Jun 04 '16 at 14:24
  • `"notice I also tagged language-agnostic and C#?"` -- I sure did. Understand that language tag spam increases attention to your question, and usually it's attention that you don't want. – Hovercraft Full Of Eels Jun 04 '16 at 14:24
  • 2
    How can a question about C# syntax be language-agnostic? – Oliver Charlesworth Jun 04 '16 at 14:24
  • @OliverCharlesworth, because this could be perfectly valid syntax in more that one language, I will change "var" to "Class" for the sake of completeness (or pernickety-ness), so to speak – Matthew Layton Jun 04 '16 at 14:25
  • @series0ne Then the only answer to that would be 'depends on how the language implements it'. – Rob Jun 04 '16 at 14:42

1 Answers1

4

Nothing. When using the object initialiser syntax { }, the () for a parameterless constructor is optional.

From the overview of C# 3.0, where this syntax was introduced:

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

You might find this related question of some interest.

Community
  • 1
  • 1
Charles Mager
  • 25,735
  • 2
  • 35
  • 45