Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 997
Masoud Zayyani Avatar asked Jan 07 '12 14:01

Masoud Zayyani


2 Answers

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

List<Student> students = RetrieveCourseStudents(cf).Cast<Student>().ToList() 
like image 78
Claus Jørgensen Avatar answered Sep 22 '22 13:09

Claus Jørgensen


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.

like image 29
George Duckett Avatar answered Sep 23 '22 13:09

George Duckett