471,330 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

About Array and IList

Hello!

The Array class implements these interfaces IClonable, IList, ICollection
and IEnumerable.
Note only interfaces are inherited and no classes so these interfaces will
be implemented in the derived class.

In Interface IList have a couple of members that is not available from the
Array object.
For example if I do the following
int[] vector = new int[5];
And then use the intellisense on instance vector I can't see for example
Add, Remove RemoveAt
there are more but these are just example of members that are not available
from the vector instance.

So my question is how is this possible?
I must have missed something here about implementing interfaces !
I have always thought that all implemented members must be accessible from
the derived class which
is not the case in this example.

//Tony

Jun 29 '08 #1
2 4291
Tony Johansson wrote:
Hello!

The Array class implements these interfaces IClonable, IList, ICollection
and IEnumerable.
Note only interfaces are inherited and no classes so these interfaces will
be implemented in the derived class.

In Interface IList have a couple of members that is not available from the
Array object.
For example if I do the following
int[] vector = new int[5];
And then use the intellisense on instance vector I can't see for example
Add, Remove RemoveAt
there are more but these are just example of members that are not available
from the vector instance.

So my question is how is this possible?
I must have missed something here about implementing interfaces !
I have always thought that all implemented members must be accessible from
the derived class which
is not the case in this example.

//Tony
They are accessible from the class, but they are implemented explicitly,
which means that you need an IList reference to access them.

This compiles:

((IList)someArray).Add(42);

Of course it throws a NotSupportedException if you try to run the code.

--
Göran Andersson
_____
http://www.guffa.com
Jun 29 '08 #2
Tony Johansson wrote:
Hello!

The Array class implements these interfaces IClonable, IList, ICollection
and IEnumerable.
Note only interfaces are inherited and no classes so these interfaces will
be implemented in the derived class.

In Interface IList have a couple of members that is not available from the
Array object.
For example if I do the following
int[] vector = new int[5];
And then use the intellisense on instance vector I can't see for example
Add, Remove RemoveAt
there are more but these are just example of members that are not available
from the vector instance.

So my question is how is this possible?
You need to cast your Array to IList first:

string[] strings = new string[] { "foo", "bar" };
IList stringList = (IList)strings;
Console.WriteLine(stringList.Contains("foo"));

That is because IList is explicitly implemented by Array.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 29 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Chris | last post: by
4 posts views Thread by Jim Shaffer | last post: by
4 posts views Thread by kin | last post: by
7 posts views Thread by Jim Lewis | last post: by
3 posts views Thread by Rafael Tejera | last post: by
37 posts views Thread by Hilton | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.