Tags: abstract, array, class, icloneable, icollection, ienumerable, ilist, inherits, net, public, sharp, specs, system, systemarray

System.Array inherits from IList ??

On .Net » .Net C# (C sharp)

6,481 words with 3 Comments; publish: Wed, 21 May 2008 23:53:00 GMT; (10078.13, « »)

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

All Comments

Leave a comment...

  • 3 Comments
    • 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]

      - mvp.net-csharp.itags.org.spam.guard.caspershouse.com

      "Chris" <christianc.net-csharp.itags.org.pandora.be> wrote in message

      news:xT4xb.44779$kk1.1558594.net-csharp.itags.org.phobos.telenet-ops.be...

      > 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

      >

      >

      >

      #1; Wed, 21 May 2008 23:54:00 GMT
    • ah yes. of course !

      Thnx for your quick response

      "Nicholas Paldino [.NET/C# MVP]" <mvp.net-csharp.itags.org.spam.guard.caspershouse.com> wrote in

      message news:eXHba6DtDHA.536.net-csharp.itags.org.tk2msftngp13.phx.gbl...

      > 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]

      > - mvp.net-csharp.itags.org.spam.guard.caspershouse.com

      > "Chris" <christianc.net-csharp.itags.org.pandora.be> wrote in message

      > news:xT4xb.44779$kk1.1558594.net-csharp.itags.org.phobos.telenet-ops.be...

      > > 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

      > >

      > >

      > >

      > >

      > >

      >

      #2; Wed, 21 May 2008 23:55:00 GMT
    • "Chris" wrote...

      > 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/default.asp?url=/library/en-us/cpref/html/frlrfsystemarrayclassilistaddtopic.asp

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

      http://tinyurl.com/wny0

      // Bjorn A

      #3; Wed, 21 May 2008 23:56:00 GMT