How to create a List with int elements from an int array in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[5]; 
            arr[0] = 14;
            arr[1] = 23;
            arr[2] = 36;
            arr[3] = 47;
            arr[4] = 50;

            List<int> list = new List<int>(arr); 

            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine($"index[{i}] = {list[i]}");
            }
        }
    }
}


/*
run:
      
index[0] = 14
index[1] = 23
index[2] = 36
index[3] = 47
index[4] = 50

*/

 



answered Dec 26, 2016 by avibootz

Related questions

1 answer 209 views
1 answer 120 views
1 answer 171 views
1 answer 170 views
170 views asked Feb 11, 2024 by avibootz
...