C# Generics: Why int[] doesn't implement IList<int>?

  • Thread starter Thread starter Sherif ElMetainy
  • Start date Start date
S

Sherif ElMetainy

Hello

I was just got VS 2005 preview, and was trying generics.

I tried the following code

int[] intArray = new int[100];
IList<int> intList = (IList<int>) intArray;

it didn't compile, also the following didn't compile

IEnumerable<int> intEnumerable = (IEnumerable<int>)intArray;

IMHO it makes perfect sense that int[] should implement IList<int>,
IEnumerable<int>, etc, since it implements the non-generic IList, and
IEnumerable.

Best regards,
Sherif
 
Please, post whidbey questions to the private whidbey NG, This NG is for
released products.

Willy.
 
Sherif,

Given that System.Array implements IList and IEnumerable, the
specialized versions should definitely be supported.

I'll file a bug. Thanks for finding it.
 
Nicholas said:
Sherif,

Given that System.Array implements IList and IEnumerable, the
specialized versions should definitely be supported.

I'll file a bug. Thanks for finding it.

I don't think this is going to be possible without creating
System.Int32Array, System.StringArray, etc.

Example:

int[] array1 = new int[100];
string[] array2 = new string[50];

Both of these objects (array1 and array2) are of type System.Array. How
can one of them implement IList<int> and the other implement
IList<string>? How could the compiler have any way of knowing which
interfaces are implemented by which instances?

There just isn't a way for two instances of the same type to implement
different interfaces; it breaks way too many OOP rules.
 
Kevin,

You misunderstood the implication that I was making. Basically, because
System.Array (the general base for all arrays) implements IList, and
IEnumerable, the specialized arrays (int[], string[], etc, etc) should also
implement the specialized versions (IList<int> and IEnumerable<int>) as
well.

Also, array1 and array2 are not of type System.Array. Rather, they
derive from System.Array. Because of this, it is easy for the derivation to
implement IList<T>.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Kevin P. Fleming said:
Nicholas said:
Sherif,

Given that System.Array implements IList and IEnumerable, the
specialized versions should definitely be supported.

I'll file a bug. Thanks for finding it.

I don't think this is going to be possible without creating
System.Int32Array, System.StringArray, etc.

Example:

int[] array1 = new int[100];
string[] array2 = new string[50];

Both of these objects (array1 and array2) are of type System.Array. How
can one of them implement IList<int> and the other implement
IList<string>? How could the compiler have any way of knowing which
interfaces are implemented by which instances?

There just isn't a way for two instances of the same type to implement
different interfaces; it breaks way too many OOP rules.
 
Nicholas said:
You misunderstood the implication that I was making. Basically, because
System.Array (the general base for all arrays) implements IList, and
IEnumerable, the specialized arrays (int[], string[], etc, etc) should also
implement the specialized versions (IList<int> and IEnumerable<int>) as
well.

Also, array1 and array2 are not of type System.Array. Rather, they
derive from System.Array. Because of this, it is easy for the derivation to
implement IList<T>.

OK, I did not realize that array1 and array2 would be instances of a
type derived from System.Array. Is this true whenever I declare any
array? I was not aware that the C# compiler created hidden types to be
used to implement arrays. I guess it must, otherwise array1[0] would be
a System.Object, not a System.Int32.

Given this information, implementing the IList and IEnumerator generics
would be relatively easy and quite useful.
 
Kevin,

This is true when you delcare any array. Your array type is T[]. The
base of T[] is System.Array.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Kevin P. Fleming said:
Nicholas said:
You misunderstood the implication that I was making. Basically, because
System.Array (the general base for all arrays) implements IList, and
IEnumerable, the specialized arrays (int[], string[], etc, etc) should also
implement the specialized versions (IList<int> and IEnumerable<int>) as
well.

Also, array1 and array2 are not of type System.Array. Rather, they
derive from System.Array. Because of this, it is easy for the derivation to
implement IList<T>.

OK, I did not realize that array1 and array2 would be instances of a
type derived from System.Array. Is this true whenever I declare any
array? I was not aware that the C# compiler created hidden types to be
used to implement arrays. I guess it must, otherwise array1[0] would be
a System.Object, not a System.Int32.

Given this information, implementing the IList and IEnumerator generics
would be relatively easy and quite useful.
 
Hello

Kevin P. Fleming said:
Nicholas Paldino [.NET/C# MVP] wrote:

There just isn't a way for two instances of the same type to implement
different interfaces; it breaks way too many OOP rules.

Although int[] and string[] inherit from System.Array, they are not the same
type. System.Array implements the non generic IList, so int[] should
implement IList<int> and string[] should implement IList<string>

Best regards,
Sherif
 
Hello

Sorry for any inconvenience, but I think people using released version of c#
are intrested in future versions as well.

Best regards,
Sherif

Willy Denoyette said:
Please, post whidbey questions to the private whidbey NG, This NG is for
released products.

Willy.

Sherif ElMetainy said:
Hello

I was just got VS 2005 preview, and was trying generics.

I tried the following code

int[] intArray = new int[100];
IList<int> intList = (IList<int>) intArray;

it didn't compile, also the following didn't compile

IEnumerable<int> intEnumerable = (IEnumerable<int>)intArray;

IMHO it makes perfect sense that int[] should implement IList<int>,
IEnumerable<int>, etc, since it implements the non-generic IList, and
IEnumerable.

Best regards,
Sherif
 
Thanks

Nicholas Paldino said:
Sherif,

Given that System.Array implements IList and IEnumerable, the
specialized versions should definitely be supported.

I'll file a bug. Thanks for finding it.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)



Sherif ElMetainy said:
Hello

I was just got VS 2005 preview, and was trying generics.

I tried the following code

int[] intArray = new int[100];
IList<int> intList = (IList<int>) intArray;

it didn't compile, also the following didn't compile

IEnumerable<int> intEnumerable = (IEnumerable<int>)intArray;

IMHO it makes perfect sense that int[] should implement IList<int>,
IEnumerable<int>, etc, since it implements the non-generic IList, and
IEnumerable.

Best regards,
Sherif
 
Hi,
Sorry, but I have to disagree,those really interested in future versions can
also subscribe to the private Whidbey NG's., and those actually having
issues with this new stuff, will find better answers in the Whidbey NG's
(MSFT PM are actively monitoring these NG's). And don't forget that we are
talking about pre-alpha code here, nothing is carved in stone yet, some
might really get confused when reading messages about un-released stuff in
public NG's.

Willy.


Sherif ElMetainy said:
Hello

Sorry for any inconvenience, but I think people using released version of
c#
are intrested in future versions as well.

Best regards,
Sherif

Willy Denoyette said:
Please, post whidbey questions to the private whidbey NG, This NG is for
released products.

Willy.

Sherif ElMetainy said:
Hello

I was just got VS 2005 preview, and was trying generics.

I tried the following code

int[] intArray = new int[100];
IList<int> intList = (IList<int>) intArray;

it didn't compile, also the following didn't compile

IEnumerable<int> intEnumerable = (IEnumerable<int>)intArray;

IMHO it makes perfect sense that int[] should implement IList<int>,
IEnumerable<int>, etc, since it implements the non-generic IList, and
IEnumerable.

Best regards,
Sherif
 
Hello

I am corrected. Sorry for the inconvenience.

Best regards,
Sherif

Willy Denoyette said:
Hi,
Sorry, but I have to disagree,those really interested in future versions can
also subscribe to the private Whidbey NG's., and those actually having
issues with this new stuff, will find better answers in the Whidbey NG's
(MSFT PM are actively monitoring these NG's). And don't forget that we are
talking about pre-alpha code here, nothing is carved in stone yet, some
might really get confused when reading messages about un-released stuff in
public NG's.

Willy.


Sherif ElMetainy said:
Hello

Sorry for any inconvenience, but I think people using released version of
c#
are intrested in future versions as well.

Best regards,
Sherif

Willy Denoyette said:
Please, post whidbey questions to the private whidbey NG, This NG is for
released products.

Willy.

Hello

I was just got VS 2005 preview, and was trying generics.

I tried the following code

int[] intArray = new int[100];
IList<int> intList = (IList<int>) intArray;

it didn't compile, also the following didn't compile

IEnumerable<int> intEnumerable = (IEnumerable<int>)intArray;

IMHO it makes perfect sense that int[] should implement IList<int>,
IEnumerable<int>, etc, since it implements the non-generic IList, and
IEnumerable.

Best regards,
Sherif
 
Back
Top