Using the 'new' modifier with interfaces at a given index like 'var b = new ISomeInterface[0];', what does it mean?

74 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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.

1
On

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)