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 504 views
504 views asked Sep 11, 2019 by avibootz
1 answer 114 views
1 answer 170 views
170 views asked Sep 10, 2020 by avibootz
1 answer 186 views
186 views asked Jan 28, 2019 by avibootz
1 answer 162 views
1 answer 110 views
1 answer 104 views
...