Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Code to Convert List to Array

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 216
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: