Home All Groups Group Topic Archive Search About
Author
17 Jan 2006 7:14 PM
haroldsphsu
Hi,

I understand that I cannot create an instance of an interface, but why
is it ok to do the following?  What is it creating?

foo = New System.ComponentModel.IEditableObject(10) {}

whereas the following will get a compiler error ('New' cannot be used
on interface):

foo = New System.ComponentModel.IEditableObject(10)

Thanks,
Harold

Author
17 Jan 2006 7:30 PM
Mattias Sjögren
>What is it creating?

An 11-element array of the interface type, just like your subject
says.


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
17 Jan 2006 7:41 PM
haroldsphsu
But why is it an error if I don't specify the empty array element
initializer?
Author
17 Jan 2006 7:49 PM
haroldsphsu
Sorry, I think I got it.  Without the array initializer, it's not
actually an array I'm creating.
Author
17 Jan 2006 7:47 PM
Herfried K. Wagner [MVP]
<haroldsp***@gmail.com> schrieb:
> I understand that I cannot create an instance of an interface, but why
> is it ok to do the following?  What is it creating?
>
> foo = New System.ComponentModel.IEditableObject(10) {}

An array with items of type 'IEditableObject' containing 11 elements with
indices 0 through 10.

> whereas the following will get a compiler error ('New' cannot be used
> on interface):
>
> foo = New System.ComponentModel.IEditableObject(10)

In this case the curly braces are required to make the compiler aware that
an array should be created instead of calling a non-existing constructor.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
17 Jan 2006 7:52 PM
haroldsphsu
Thanks Herfried and Mattias!