How to throw an Ilist into an ArrayList?

Can I transform an IList into an ArrayList ?

if so, what should I do?

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

Is it correct?

has an error:

Unable to create object of type

+4
source share
8 answers

It is all about polymorphism. ArrayList is an implementation from the IList interface .

  IList iList = new ArrayList(); 

The static type is from the iList IList variable, but it refers to an ArrayList!

There is no real drop from IList to ArrayList, because you cannot create / create objects from an interface or an abstract class.

0
source

As pointed out in the comments, you should use shared collections instead

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

You can use alls for an ArrayList if it is already an ArrayList , that is, if the object returned by RetrieveCourseStudents is an ArrayList .

If this is not the case, you need to create a new object, fortunately ArrayList has a constructor that can do this: new ArrayList(RetrieveCourseStudents(cf))


It is worth noting that now you should use generics (such as List<T> ) instead of ArrayList , so if you do not need to interact with some old code that cannot be updated, I would stay away from it.

+4
source

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

eg

 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(); } 
0
source

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

You can use the OrderBy LINQ Extension Method (and the type of variable you are using - IList also not suitable).

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

 var alls = RetrieveCourseStudents(cf); var orderedAlls = alls.OrderBy(cs => cs.CourseStudentProperty); 
0
source
 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; } } } 
0
source

just use this simple code :)

 (From x In list).ToArray 
0
source

You can use the LINQ Union extension.

Note that you can combine any type of IEnumerable with this method (Array, IList, etc.), so you donโ€™t have to worry about the Add method. You must understand that LINQ creates consistent results, so you need to use "ToList ()", "ToDictionary ()" or whatever if you want to manipulate the collection later.

  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 
0
source

Source: https://habr.com/ru/post/1389812/


All Articles