Convert IList to array in C#

前端 未结 4 1961
后悔当初
后悔当初 2021-01-08 00:24

I want to convert IList to array: Please see my code:

IList list = new ArrayList();
list.Add(1);
Array array = new Array[list.Count];
list.CopyTo(array, 0);
         


        
4条回答
  •  悲哀的现实
    2021-01-08 00:55

    I'm surprised that

     Array array = new Array[list.Count];
    

    even compiles but it does not do what you want it to. Use

     object[] array = new object[list.Count];
    

    And, standard remark: if you can use C#3 or later, avoid ArrayList as much as possible. You'll probably be happier with a List

提交回复
热议问题