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

后端 未结 3 1502
遇见更好的自我
遇见更好的自我 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:09

    Whilst this doesn't answer your question directly, if you are using .NET 3.5 you can include the namespace;

    using System.Linq;
    

    which will then allow you to use a Count() method, similar to when casting your int array as an ICollection.

    using System.Linq;
    
    int[] arr = new int[5];
    int int_count = arr.Count();
    

    You also then have a whole host of nice functions you can use in Linq too :)

提交回复
热议问题