0

I've got a basic conceptual doubt and have been looking for an answer for some time now. I've initialized a list in the following manner :

List<string> list = new List<string>() { "a", "b", "c" };

Just this once I missed the () and initialized it in the following manner instead :

List<string> list = new List<string> { "a", "b", "c" };

No compile time or run time error was thrown. so I want to know if the second method is right? And what is the difference between the two?

1 Answers1

1

Both does the same job, where () is optional

In your first example, the compiler knows that you're calling the default constructor

  List<string> list = new List<string> { "a", "b", "c" };

In the second, you explicitly call the default constructor

  List<string> list = new List<string>() { "a", "b", "c" };
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396