Monday, 15 July 2013

c# list to array and array to list convert

List to Array

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

Array to List

 string[] s = new string[]
 {
     "one",
     "two",
     "three",
     "four",
     "five"
 };
 
 // Convert with new List constructor.
 List<string> l = new List<string>(s); 
 List<string> l2 = s.ToList();

No comments:

Post a Comment