List -> Array

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

    #1

    List -> Array

    Hello,

    What's the easiest way to convert List -> Array in c#? Say for example the
    same thing like in Java:

    List list = new ArrayList();

    Item[] items = (Item[]) list.toArray(ne w Item[list.size()]);

    Is there a way to do this?

    Thanks heaps,

    /m


  • Daniel O'Connell

    #2
    Re: List -> Array


    "muscha" <muscha@no.spam .net> wrote in message
    news:%23OeH15bf DHA.3024@tk2msf tngp13.phx.gbl. ..[color=blue]
    > Hello,
    >
    > What's the easiest way to convert List -> Array in c#? Say for example the
    > same thing like in Java:
    >
    > List list = new ArrayList();
    >
    > Item[] items = (Item[]) list.toArray(ne w Item[list.size()]);
    >[/color]

    Pretty similar over all. If your list is an ArrayList, you can call
    ArrayList.ToArr ay directly, if not, use the ArrayList(IColl ection)
    constructor:
    //assumes you have an IList list defined.
    //the new array list is used to provide ToArray, not pretty but ToArray
    isn't provided in
    //IList or ICollection
    ArrayList arr = new ArrayList(list) ;
    Item[] items = (Item[])arr.ToArray(ty peof(Item));
    [color=blue]
    > Is there a way to do this?
    >
    > Thanks heaps,
    >
    > /m
    >
    >[/color]


    Comment

    • Jon Skeet

      #3
      Re: List -&gt; Array

      muscha <muscha@no.spam .net> wrote:[color=blue]
      > What's the easiest way to convert List -> Array in c#? Say for example the
      > same thing like in Java:
      >
      > List list = new ArrayList();
      >
      > Item[] items = (Item[]) list.toArray(ne w Item[list.size()]);
      >
      > Is there a way to do this?[/color]

      Yes - and it's actually rather nicer in .NET/C#:

      ArrayList list = new ArrayList();
      ....

      Item[] items = (Item[]) list.ToArray (typeof(Item));

      Note that it needs to be declared as ArrayList rather than IList, as
      IList doesn't have the ToArray method, unfortunately.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • muscha

        #4
        Re: List -&gt; Array

        > > List list = new ArrayList();[color=blue][color=green]
        > >
        > > Item[] items = (Item[]) list.toArray(ne w Item[list.size()]);
        > >
        > > Is there a way to do this?[/color]
        >
        > Yes - and it's actually rather nicer in .NET/C#:
        >
        > ArrayList list = new ArrayList();
        > ...
        >
        > Item[] items = (Item[]) list.ToArray (typeof(Item));
        >
        > Note that it needs to be declared as ArrayList rather than IList, as
        > IList doesn't have the ToArray method, unfortunately.[/color]

        Ah that's why. I wonder why ToArray() is not defined in IList?

        /m


        Comment

        Working...