C# .NET - Convert list of object to array of objects

Asked By mostafa hamdy on 13-Sep-10 05:38 AM
Hello all
I have some list of objects and I want to convert it to array of objects , how can I do that ?
please if any body can help me please send me or tell me about some url may help me in doing that
regards
Mostafa
Sagar P replied to mostafa hamdy on 13-Sep-10 05:47 AM
You can use List.ToArray() method for that like;

    // A.
      // New list here.

      List<string> l = new List<string>();
      l.Add("one");
      l.Add("two");
      l.Add("three");
      l.Add("four");
      l.Add("five");

      // B.
      string[] s = l.ToArray();

This will give you string array s which will have all the elements for list l
Kirtan Patel replied to mostafa hamdy on 13-Sep-10 09:26 AM
        
        Form
 f = Form1;
      Form f2 = Form2;
      List<Form> list = new List<Form>();
      list.Add(f);
      list.Add(f2);

      Form[] farray = list.ToArray();
Goniey N (Mr. G) replied to mostafa hamdy on 13-Sep-10 11:51 PM
-- Us Below Code :

01.List<string> stringlist = new List<string>();
02.stringlist.Add("Obj1");
03.stringlist.Add("Obj2");
04.stringlist.Add("Obj3");
05. 
06.//You Can Store Your List In The Arraylist...
07.System.Collections.ArrayList MyList = new System.Collections.ArrayList();
08. 
09.//It Will Add All List Items Into Arraylist...
10.MyList.AddRange(stringlist.ToArray());
11. 
12. 
13.//Like Below You Can Access The Items Of The Arraylist Using It's Index...
14.for (int i = 0; i < MyList.Count; i++)
15.{
16.  //Show Message one By One For Each List Items(Your Objects)...
17.  MessageBox.Show(MyList[i].ToString());
18.}




-- It Will Work 100%...

-- Hope This Will Help You...