TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Convert ArrayList to List

Convert an ArrayList to a List. Introduce a generic extension method.
Convert ArrayList, List. An ArrayList can be converted into a List. This can be done in a variety of ways. One way uses the extension method syntax. It yields an effective ToList method for the ArrayList type.ArrayListConvert ArrayList, Array
To start, this program introduces the static Extensions class and the ToList generic method inside that class. ToList here is a generic extension method. It has a type parameter T.ExtensionGeneric Class, Method

This: The "this" modifier is used on the ArrayList argument to specify that the method is an extension.

And: Inside ToList, we allocate a List with the correct capacity, and then simply copy each element.

Main: We test the ToList method on an ArrayList instance. You can see that the method works correctly for the List of ints.

C# program that converts ArrayList to List using System; using System.Collections; using System.Collections.Generic; static class Extensions { /// <summary> /// Convert ArrayList to List. /// </summary> public static List<T> ToList<T>(this ArrayList arrayList) { List<T> list = new List<T>(arrayList.Count); foreach (T instance in arrayList) { list.Add(instance); } return list; } } class Program { static void Main() { // Create ArrayList. ArrayList arrayList = new ArrayList(); arrayList.Add(1); arrayList.Add(2); arrayList.Add(3); // Use extension method. List<int> list = arrayList.ToList<int>(); foreach (int value in list) { Console.WriteLine(value); } } } Output 1 2 3
Notes, testing. I also tested the method using string literals in the ArrayList and it worked correctly. Just change the List<int> to a List<string> and also change int to string in the other places.String LiteralInt, uintStringsList
Summary. With ToList we converted an ArrayList to a List. Another approach is to convert the ArrayList to an Array, and then use the new List constructor with that Array instance as the parameter. The approach here is not necessarily better.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf