PC Review


Reply
Thread Tools Rate Thread

About Array and IList

 
 
Tony Johansson
Guest
Posts: n/a
 
      29th Jun 2008
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



 
Reply With Quote
 
 
 
 
Göran Andersson
Guest
Posts: n/a
 
      29th Jun 2008
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
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      29th Jun 2008
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/
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Int array or IList array. Join shapper Microsoft C# .NET 5 17th Dec 2009 01:13 AM
What is the relationships between IList, Collection and Array ad Microsoft C# .NET 3 16th Nov 2005 06:00 AM
System.Array inherits from IList ??? Chris Microsoft VB .NET 2 5th Dec 2003 04:58 PM
System.Array inherits from IList ??? Chris Microsoft C# .NET 3 26th Nov 2003 05:05 PM
System.Array and IList - language/framework design question emma middlebrook Microsoft C# .NET 4 8th Aug 2003 08:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:30 PM.