System.Array inherits from IList ???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    #1

    System.Array inherits from IList ???

    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:Get Enumerator()
    is an abstract function as well and I can use that one, no problem.

    Thanks in advance

    Chris





  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: System.Array inherits from IList ???

    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@spam.guard. caspershouse.co m

    "Chris" <christianc@pan dora.be> wrote in message
    news:xT4xb.4477 9$kk1.1558594@p hobos.telenet-ops.be...[color=blue]
    > 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'[/color]
    does[color=blue]
    > 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[/color]
    them[color=blue]
    > yourself (such as IList::Add) but then again , IEnumerable:Get Enumerator()
    > is an abstract function as well and I can use that one, no problem.
    >
    > Thanks in advance
    >
    > Chris
    >
    >
    >
    >
    >[/color]


    Comment

    • Chris

      #3
      Re: System.Array inherits from IList ???

      ah yes. of course !
      Thnx for your quick response

      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
      message news:eXHba6DtDH A.536@tk2msftng p13.phx.gbl...[color=blue]
      > 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[/color]
      array,[color=blue]
      > 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@spam.guard. caspershouse.co m
      >
      > "Chris" <christianc@pan dora.be> wrote in message
      > news:xT4xb.4477 9$kk1.1558594@p hobos.telenet-ops.be...[color=green]
      > > 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'[/color]
      > does[color=green]
      > > not contain a definition for 'Add'
      > > Array.Add(2) --> NOT POSSIBLE Compiler error :[/color][/color]
      'System.Array'[color=blue][color=green]
      > > 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[/color]
      > them[color=green]
      > > yourself (such as IList::Add) but then again ,[/color][/color]
      IEnumerable:Get Enumerator()[color=blue][color=green]
      > > is an abstract function as well and I can use that one, no problem.
      > >
      > > Thanks in advance
      > >
      > > Chris
      > >
      > >
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Bjorn Abelli

        #4
        Re: System.Array inherits from IList ???

        "Chris" wrote...
        [color=blue]
        > 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 ?[/color]

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

        It *is* implemented.

        <quote>
        Array.IList.Add

        Implements IList.Add. Always throws NotSupportedExc eption.

        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/de...staddtopic.asp

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

        http://tinyurl.com/wny0

        // Bjorn A


        Comment

        Working...