Masoud Zayyani
Masoud Zayyani

Reputation: 157

How To Cast Ilist To ArrayList?

Can I Cast IList into ArrayList?

if yes what should I do?

IList alls = RetrieveCourseStudents(cf);
ArrayList a = (ArrayList)alls;

Is that correct?

is has error:

Unable to cast object of type

Upvotes: 5

Views: 7641

Answers (8)

Ben Stabile
Ben Stabile

Reputation: 173

You could use the LINQ Union extension.

Note that you can combine any type of IEnumerable with this technique (Array, IList, and so on), so you don't need to worry about an "Add" method. You do have to understand that LINQ is producing immutable results, so you then need to use "ToList()", "ToDictionary()", or whatever, if you want to subsequently manipulate the collection.

    var list =
        (IList<Student>) new [] 
        {
            new Student {FirstName = "Jane"}, 
            new Student {FirstName = "Bill"},
        };

    var allStudents = list.Union(
        new [] {new Student {FirstName = "Clancey"}})
           .OrderBy(s => s.FirstName).ToList();

    allStudents[0].FirstName = "Billy";

    foreach (var s in allStudents)
    {
        Console.WriteLine("FirstName = {0}", s.FirstName);
    }

Output:

FirstName = Billy
FirstName = Clancey
FirstName = Jane

Upvotes: 0

Mohammad Kani
Mohammad Kani

Reputation: 942

just use this simple code :)

(From x In list).ToArray

Upvotes: 0

Raghupathi Kamuni
Raghupathi Kamuni

Reputation: 31

using System;
using System.Collections;
using System.Collections.Generic;
namespace MyILists
{
class Program
{
    static void Main(string[] args)
    {
        IList<int> intArrayList = new ArrayList().ToIList<int>();
        intArrayList.Add(1);
        intArrayList.Add(2);
        //intArrayList.Add("Sample Text"); // Will not compile

        foreach (int myInt in intArrayList)
        {
            Console.WriteLine(" Number : " + myInt.ToString());
        }
        Console.Read();
    }      
}

public static class MyExtensions
{
    public static IList<T> ToIList<T>(this ArrayList arrayList)
    {
        IList<T> list = new List<T>(arrayList.Count);
        foreach (T instance in arrayList)
        {
            list.Add(instance);
        }
        return list;
    }

}
}

Upvotes: 0

Oded
Oded

Reputation: 499062

Since you commented that you simply want to order the returned list (which in another comment you say is of type EntityCollection<CourseStudent>) - you do not need to cast to an ArrayList but simply use the value directly.

You can use the OrderBy LINQ extension method (and the variable type you use - IList is also not suitable).

This would work for your needs (where CourseStudentProperty is a property of CourseStudent):

var alls = RetrieveCourseStudents(cf);
var orderedAlls = alls.OrderBy(cs => cs.CourseStudentProperty);

Upvotes: 0

Gero
Gero

Reputation: 13553

This is all about polymorphism. ArrayList is an implementation from the Interface IList.

 IList iList = new ArrayList();  

The static type from the variable iList is IList but it references an ArrayList Object!

There is no real casting from IList to ArrayList because you can not instantiate/create Object from Interface or abstract Class.

Upvotes: 0

Manas
Manas

Reputation: 2542

Yes, we can cast IList to ArrayList only if RetrieveCourseStudents(cf) returns type of Arraylist.

for example

static void Main(string[] args)
        {
            IList test1 = GetList();
            IList test2= GetIList();
            ArrayList list1 = (ArrayList)test1; // Fails
            ArrayList list2 = (ArrayList)test2;  // Passes

            Console.ReadKey();
        }

        private static IList GetIList()
        {
            return new ArrayList();
        }

        private static IList GetList()
        {
            return new CustomList();
        }

Upvotes: 0

George Duckett
George Duckett

Reputation: 32428

You'd only be able to cast alls to an ArrayList if it already is an ArrayList, i.e. if the object returned by RetrieveCourseStudents is an ArrayList.

If it isn't then you need to create a new object, luckly ArrayList has a constructor that can do this: new ArrayList(RetrieveCourseStudents(cf))


It's worth noting that you should be using generics (such as List<T>) instead of ArrayList now, so unless you need to interact with some old code that can't be updated, i'd stay away from it.

Upvotes: 4

Claus J&#248;rgensen
Claus J&#248;rgensen

Reputation: 26338

As suggested in the comments, you should consider using generic collections instead

List<Student> students = RetrieveCourseStudents(cf).Cast<Student>().ToList() 

Upvotes: 4

Related Questions