System.Array inherits from IList ???

C

Chris

Hi,

the specs for System.Array are :
public abstract class Array : ICloneable, IList, ICollection,
IEnumerable

but I can't use any of the functions presented by IList in my code

System.Array numbers = new int[5]{1,2,5,6,7};
numbers.Add(1) --> NOT POSSIBLE Compiler error : 'System.Array' does
not contain a definition for 'Add'
Array.Add(2) --> NOT POSSIBLE Compiler error : 'System.Array'
does not contain a definition for 'Add'

nor some belonging to ICollection : SyncRoot:yes, Count:no
numbers.Count --> NOT POSSIBLE

IEnumerable : no problem

What is reasoning behind all this ?
Already does intelisense not show those functions. But why not ? if the
specs make you suppose you could use them.

I thought maybe 'cause the functions are abstract so you must implement them
yourself (such as IList::Add) but then again , IEnumerable:GetEnumerator()
is an abstract function as well and I can use that one, no problem.

Thanks in advance

Chris
 
N

Nicholas Paldino [.NET/C# MVP]

Chris,

If a class implements an interface, it doesn't have to expose those
members publically. In order to access the IList functionality on an array,
you must assign to an IList reference, like so:

// Get the IList.
IList pobjArray = new int[1]{1};

You can set pobjArray equal to any array reference.

Hope this helps.
 
B

Bjorn Abelli

...
the specs for System.Array are :
public abstract class Array : ICloneable,
IList, ICollection,
IEnumerable

but I can't use any of the functions
presented by IList in my code

What is reasoning behind all this ?

An array is a special case of an IList, which have been handled based on the
fact that an array has a fixed size.
I thought maybe 'cause the functions are abstract so
you must implement them yourself (such as IList::Add) ...

It *is* implemented.

<quote>
Array.IList.Add

Implements IList.Add. Always throws NotSupportedException.

Array has a fixed size; therefore, elements cannot be added or removed. Use
SetValue to change the value of an existing element.

</quote>

You can read more in the documentation:

http://msdn.microsoft.com/library/d...f/html/frlrfsystemarrayclassilistaddtopic.asp

If the above link wraps, you can use this instead:

http://tinyurl.com/wny0

// Bjorn A
 
C

Chris

ah yes. of course !
Thnx for your quick response

Nicholas Paldino said:
Chris,

If a class implements an interface, it doesn't have to expose those
members publically. In order to access the IList functionality on an array,
you must assign to an IList reference, like so:

// Get the IList.
IList pobjArray = new int[1]{1};

You can set pobjArray equal to any array reference.

Hope this helps.


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

Chris said:
Hi,

the specs for System.Array are :
public abstract class Array : ICloneable, IList, ICollection,
IEnumerable

but I can't use any of the functions presented by IList in my code

System.Array numbers = new int[5]{1,2,5,6,7};
numbers.Add(1) --> NOT POSSIBLE Compiler error : 'System.Array' does
not contain a definition for 'Add'
Array.Add(2) --> NOT POSSIBLE Compiler error : 'System.Array'
does not contain a definition for 'Add'

nor some belonging to ICollection : SyncRoot:yes, Count:no
numbers.Count --> NOT POSSIBLE

IEnumerable : no problem

What is reasoning behind all this ?
Already does intelisense not show those functions. But why not ? if the
specs make you suppose you could use them.

I thought maybe 'cause the functions are abstract so you must implement them
yourself (such as IList::Add) but then again , IEnumerable:GetEnumerator()
is an abstract function as well and I can use that one, no problem.

Thanks in advance

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top