ToArray() ?

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

    #1

    ToArray() ?

    Hi

    I call a method which returns an IList containing a list of one type of
    object. How do I best convert this IList to an array?

    I can see that the ArrayList class has a ToArray() method - but this is not
    part of IList.

    (I do in fact know that the method I call actually returns an ArrayList as I
    have looked in the source code, but technically I really only know it is an
    IList).

    Thanks,
    Peter


  • Mattias Sjögren

    #2
    Re: ToArray() ?

    [color=blue]
    >I call a method which returns an IList containing a list of one type of
    >object. How do I best convert this IList to an array?[/color]

    IList list = ...
    Foo[] foos = new Foo[list.Count];
    for ( int i = 0; i < foos.Length; i++ )
    foos[i] = (Foo)list[i];



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Peter Kirk

      #3
      Re: ToArray() ?


      "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> skrev i en meddelelse
      news:OyigwXMbFH A.2996@TK2MSFTN GP10.phx.gbl...[color=blue]
      >[color=green]
      >>I call a method which returns an IList containing a list of one type of
      >>object. How do I best convert this IList to an array?[/color]
      >
      > IList list = ...
      > Foo[] foos = new Foo[list.Count];
      > for ( int i = 0; i < foos.Length; i++ )
      > foos[i] = (Foo)list[i];[/color]

      Well, yeah. I just thought there might be an "in bult" method a la
      ArrayList's ToArray().

      Thanks,
      Peter


      Comment

      • Peter Rilling

        #4
        Re: ToArray() ?

        How about using ICollection.Cop yTo(...). IList implements ICollection.


        "Peter Kirk" <pk@alpha-solutions.dk> wrote in message
        news:epPKveMbFH A.2124@TK2MSFTN GP14.phx.gbl...[color=blue]
        >
        > "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> skrev i en meddelelse
        > news:OyigwXMbFH A.2996@TK2MSFTN GP10.phx.gbl...[color=green]
        > >[color=darkred]
        > >>I call a method which returns an IList containing a list of one type of
        > >>object. How do I best convert this IList to an array?[/color]
        > >
        > > IList list = ...
        > > Foo[] foos = new Foo[list.Count];
        > > for ( int i = 0; i < foos.Length; i++ )
        > > foos[i] = (Foo)list[i];[/color]
        >
        > Well, yeah. I just thought there might be an "in bult" method a la
        > ArrayList's ToArray().
        >
        > Thanks,
        > Peter
        >
        >[/color]


        Comment

        • Daniel O'Connell [C# MVP]

          #5
          Re: ToArray() ?


          "Peter Kirk" <pk@alpha-solutions.dk> wrote in message
          news:u0yawFMbFH A.1404@TK2MSFTN GP09.phx.gbl...[color=blue]
          > Hi
          >
          > I call a method which returns an IList containing a list of one type of
          > object. How do I best convert this IList to an array?
          >[/color]

          You can load it into an ArrayList and uses ArrayLists.ToAr ray() if you
          aren't worried about performance, otherwise I'd recommend writing a ToArray
          implemetnation that takes an IList. Its not terribly hard to do, especially
          if you know the type the list is going to be at all times.



          Comment

          Working...