I can not access Count property of the array but through casting to ICollection !

后端 未结 3 1457
遇见更好的自我
遇见更好的自我 2021-01-23 08:26
        int[] arr = new int[5];
        Console.WriteLine(arr.Count.ToString());//Compiler Error
        Console.WriteLine(((ICollection)arr).Count.ToString());//works p         


        
3条回答
  •  再見小時候
    2021-01-23 09:10

    While System.Array implement the ICollection interface it doesn't directly expose the Count property. You can see the explicit implementation of ICollection.Count in the MSDN documentation here.

    The same applies to IList.Item.

    Take look at this Blog entry for more details on explicit and implicit interface implementation: Implicit and Explicit Interface Implementations

提交回复
热议问题