Convert List<string> to ArrayList in C#

Convert List<string> to ArrayList in C#

To convert a List<string> to an ArrayList in C#, you can use the ArrayList constructor that accepts an ICollection as a parameter. Since List<string> implements ICollection<string>, you can directly pass the List<string> to the ArrayList constructor for conversion.

Here's an example of how to convert a List<string> to an ArrayList:

using System; using System.Collections; using System.Collections.Generic; class Program { static void Main() { // Create a List<string> List<string> stringList = new List<string> { "apple", "banana", "orange" }; // Convert the List<string> to ArrayList ArrayList arrayList = new ArrayList(stringList); // Print the elements of the ArrayList foreach (var item in arrayList) { Console.WriteLine(item); } } } 

Output:

apple banana orange 

In the above example, we create a List<string> called stringList and initialize it with some string values. Then, we pass the stringList as a parameter to the ArrayList constructor, which converts it to an ArrayList named arrayList.

Please note that ArrayList is a non-generic collection that can hold objects of any type, so it is not type-safe like List<string>. If you are using a .NET version that supports generics (C# 2.0 and later), it is generally recommended to use generic collections like List<T> instead of non-generic collections like ArrayList, as they offer better type safety and performance.

Examples

  1. Convert List<string> to ArrayList C#:

    List<string> stringList = GetStringList(); ArrayList arrayList = new ArrayList(stringList); 

    Description: Use the ArrayList constructor that takes an ICollection to directly convert a List<string> to an ArrayList.

  2. Convert List<string> to ArrayList with AddRange C#:

    List<string> stringList = GetStringList(); ArrayList arrayList = new ArrayList(); arrayList.AddRange(stringList); 

    Description: Use the AddRange method to add all elements from the List<string> to the ArrayList.

  3. Convert List<string> to ArrayList with ForEach Loop C#:

    List<string> stringList = GetStringList(); ArrayList arrayList = new ArrayList(); stringList.ForEach(item => arrayList.Add(item)); 

    Description: Use a ForEach loop to iterate through the List<string> and add each element to the ArrayList.

  4. Convert List<string> to ArrayList with ToArrayList Extension Method C#:

    List<string> stringList = GetStringList(); ArrayList arrayList = stringList.ToArrayList(); public static ArrayList ToArrayList<T>(this IEnumerable<T> collection) { return new ArrayList(collection); } 

    Description: Create an extension method ToArrayList that uses the ArrayList constructor, making the conversion more readable.

  5. Convert List<string> to ArrayList with ConvertAll C#:

    List<string> stringList = GetStringList(); ArrayList arrayList = new ArrayList(stringList.ConvertAll(item => (object)item)); 

    Description: Use ConvertAll to convert each element of the List<string> to an object before creating the ArrayList.

  6. Convert List<string> to ArrayList with LINQ C#:

    List<string> stringList = GetStringList(); ArrayList arrayList = new ArrayList(stringList.Cast<object>().ToList()); 

    Description: Use LINQ's Cast method to convert each element to an object and then create the ArrayList.

  7. Convert List<string> to ArrayList with Type Mismatch Handling C#:

    List<string> stringList = GetStringList(); ArrayList arrayList = new ArrayList(); stringList.ForEach(item => { if (item != null) arrayList.Add(item); }); 

    Description: Use a ForEach loop to add non-null elements to the ArrayList, handling potential type mismatches.

  8. Convert List<string> to ArrayList with Null Handling C#:

    List<string> stringList = GetStringList(); ArrayList arrayList = new ArrayList(stringList.Where(item => item != null).ToList()); 

    Description: Use Where to filter out null elements before creating the ArrayList.

  9. Convert List<string> to ArrayList with Constructor and Capacity C#:

    List<string> stringList = GetStringList(); ArrayList arrayList = new ArrayList(stringList.Count); arrayList.AddRange(stringList); 

    Description: Pre-allocate the capacity of the ArrayList using the constructor, and then use AddRange to add elements.

  10. Convert List<string> to ArrayList with ArrayList.AddRange C#:

    List<string> stringList = GetStringList(); ArrayList arrayList = new ArrayList(); arrayList.AddRange(stringList.ToArray()); 

    Description: Use AddRange with the ToArray method to add all elements from the List<string> to the ArrayList.


More Tags

apache-kafka-security resteasy infinite-loop initialization avro datacontractjsonserializer jpanel blink zone.js jvisualvm

More C# Questions

More Date and Time Calculators

More Weather Calculators

More Electronics Circuits Calculators

More Organic chemistry Calculators