How to convert an array to a list in C#

4 Answers

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

class Program
{
    static void Main() {
        int[] arr = {5, 6, 1, 4, 3, 0, 8};
        
        List<int> lst = new List<int>();
        
        foreach (int n in arr)
                lst.Add(n);
 
        foreach (int n in lst) {
            Console.Write(n + ", ");
        }
    }
}
 
 
 
 
/*
run:
 
5, 6, 1, 4, 3, 0, 8, 
 
*/

 



answered Sep 22, 2020 by avibootz
0 votes
using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main() {
        int[] arr = {5, 6, 1, 4, 3, 0, 8};
        
        List<int> lst = arr.ToList();
        
        foreach (int n in lst) {
            Console.Write(n + ", ");
        }
    }
}
 
 
 
 
/*
run:
 
5, 6, 1, 4, 3, 0, 8, 
 
*/

 



answered Sep 22, 2020 by avibootz
0 votes
using System;
using System.Collections.Generic;

class Program
{
    static void Main() {
        int[] arr = {5, 6, 1, 4, 3, 0, 8};
        
        List<int> lst = new List<int>(arr);
        
        foreach (int n in lst) {
            Console.Write(n + ", ");
        }
    }
}
 
 
 
 
/*
run:
 
5, 6, 1, 4, 3, 0, 8, 
 
*/

 



answered Sep 22, 2020 by avibootz
0 votes
using System;
using System.Collections.Generic;

class Program
{
    static void Main() {
        int[] arr = {5, 6, 1, 4, 3, 0, 8};
        
        List<int> lst = new List<int>();
        
        lst.AddRange(arr);
        
        foreach (int n in lst) {
            Console.Write(n + ", ");
        }
    }
}
 
 
 
 
/*
run:
 
5, 6, 1, 4, 3, 0, 8, 
 
*/

 



answered Sep 22, 2020 by avibootz
...