0

I've come across the following C# syntax for the first time, I would have discarded it as a syntax error except that VS is absolutely happy with it and compiles.

var a = new ISomeInterface[0];

The interface is declared as

public interface ISomeInterface
{
}

Links to further reading are also highly appreciated.

Giuseppe Romagnuolo
  • 3,362
  • 2
  • 30
  • 38
  • 2
    Move your mouse over the `var` keyword: it'll show you the actual type (i.e. an array) – ken2k Aug 12 '14 at 12:01

2 Answers2

4

You've created an array of ISomeInterface.

This is the same as declaring any other array, such as:

string[] a = new string[0];

I kinda did a double-take on that at first too, because at first glance it appeared the code was instantiating an interface, something you can't normally do.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
2

It is creating a new array (zero-length), not a new instance of the interface. Incidentally, you can actually new an interface... under the right conditions ;p (COM attributes)

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • I don't know if I'm more honored to have my question answered by Marc Gravell himself or more ashamed in realising I have asked such a silly question! Anyway thanks! =) – Giuseppe Romagnuolo Aug 12 '14 at 12:10