Hi Friends,
Today i will tell you about how we can convert a list to array. We can use 2 methods to convert list to array
Method 1:
//Create a list object
List<string> objList = new List<string>();
//Add Items to List
objList.Add("Test1");
objList.Add("Test2");
objList.Add("Test3");
objList.Add("Test4");
objList.Add("Test5");
objList.Add("Test6");
objList.Add("Test7");
//Convert List to Array
string[] objArray = objList.ToArray();
Here .ToArray() will convert list to array.
Method 2:
//Create a list object
List<string> objList = new List<string>();
//Add Items to List
objList.Add("Test1");
objList.Add("Test2");
objList.Add("Test3");
objList.Add("Test4");
objList.Add("Test5");
objList.Add("Test6");
objList.Add("Test7");
//Convert List to Array
string[] strings = new string[objList.Count];
objList.CopyTo(strings, 0);
Thanks for reading this.
Happy Coding !!
0 Comment(s)