-1

When initializing a new List in C#, both of following will compile:

(1) List<string> s = new List<string>() { "value" };

and

(2) List<string> s = new List<string> { "value" };

What is the difference between case 1 and case 2?

enb081
  • 3,831
  • 11
  • 43
  • 66
  • 11
    There is none. It's just that the `()` is optional for that declaration. (Resharper warns about the redundant `()`.) – Matthew Watson Apr 23 '15 at 07:49
  • See [duplicate](http://stackoverflow.com/questions/3661025/why-are-c-sharp-3-0-object-initializer-constructor-parentheses-optional) (which talks about object initializers, which are in the same ball park), which links to [Ambiguous Optional Parentheses, Part One](http://blogs.msdn.com/b/ericlippert/archive/2010/09/20/ambiguous-optional-parentheses.aspx). Eric Lippert's blogs explain very well the "why" rationale behind language design. – CodeCaster Apr 23 '15 at 07:57

4 Answers4

1

This rings true for any type. Generally, you specify the () if you need to pass something to the types constructor that it doesn't expose for setting publically. If there's no parameters needed, the () are just fluff.

Consider the scenario that you may want to do some additional validation/logic to a property and you don't allow direct manipulation of it:

public class Foo
{
    private string Bar { get; set; }

    public string FooBar { get; set; }

    public Foo (string bar)
    {
        Bar = bar + "foo";
    }
}

So this is allowed:

var foo = new Foo("bar")
{
    FooBar = "foobar"
};

Yet this isn't:

var foo = new Foo
{
    Bar = "bar",
    FooBar = "foobar"
};

There are some C# types that only allow you to set certain properties within the constructor.

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
1

There is no difference. It will be translated as:

List<string> s1 = new List<string>();
s1.Add("value");
List<string> s2 = new List<string>();
s2.Add("value");
Vano Maisuradze
  • 5,829
  • 6
  • 45
  • 73
1

From 7.6.10.1 Object creation expressions of the C# reference 5.0:

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.

xanatos
  • 109,618
  • 12
  • 197
  • 280
Rawling
  • 49,248
  • 7
  • 89
  • 127
0

The latter is called Object Initialization. You may also use MyClass m = new MyClass { MyVar = 3 };

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • 3
    *The latter is used the Field Initializer* ? What? [Field Initialization](https://msdn.microsoft.com/en-us/library/aa645759%28v=vs.71%29.aspx) – Sriram Sakthivel Apr 23 '15 at 07:51
  • 1
    It's not called Field Initialization. It's called a collection initializer. The alternative you give is called an object initializer. Field initializers are the default values you give for fields in a class declaration. – Rawling Apr 23 '15 at 07:56
  • @Rawling Indeed, I´ve changed this. – MakePeaceGreatAgain Apr 23 '15 at 08:01