How to convert ArrayList to List in C#

2 Answers

0 votes
using System;
using System.Collections;
using System.Collections.Generic;
 
static class LST {
    public static List<T> ToList<T>(this ArrayList al) {
        List<T> list = new List<T>(al.Count);
        
        foreach (T n in al) {
            list.Add(n);
        }
        return list;
    }
}
 
class Program {
    static void Main() {
        ArrayList arrayList = new ArrayList(){"c++", "c#", "c", "java"};
 
        List<string> list = arrayList.ToList<string>();
         
        foreach (string n in list) {
            Console.WriteLine(n);
        }
    }
}
 
 
 
 
/*
run:
 
c++
c#
c
java
 
*/

 



answered Feb 9, 2017 by avibootz
edited May 9, 2024 by avibootz
0 votes
using System;
using System.Collections;
using System.Collections.Generic;
 
static class LST {
    public static List<T> ToList<T>(this ArrayList al) {
        List<T> list = new List<T>(al.Count);
        
        foreach (T n in al) {
            list.Add(n);
        }
        return list;
    }
}
 
class Program {
    static void Main() {
        ArrayList arrayList = new ArrayList(){4, 1, 9, 5};
 
        List<int> list = arrayList.ToList<int>();
         
        foreach (int n in list) {
            Console.WriteLine(n);
        }
    }
}
 
 
 
 
/*
run:
 
4
1
9
5
 
*/

 



answered Feb 9, 2017 by avibootz
edited May 9, 2024 by avibootz

Related questions

7 answers 862 views
862 views asked Sep 11, 2019 by avibootz
1 answer 235 views
1 answer 279 views
279 views asked Sep 10, 2020 by avibootz
1 answer 319 views
319 views asked Jan 28, 2019 by avibootz
1 answer 255 views
1 answer 229 views
1 answer 188 views
...