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
As suggested in the comments, you should consider using generic collections instead
List<Student> students = RetrieveCourseStudents(cf).Cast<Student>().ToList()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With