A generic List.Count gives a System.ArgumentException
I have the following class:
public class SimContactDetails2G
{
public String AbreviatedName { get; set; }
public String DialingNumber { get; set; }
public int Index { get; set; }
public String Surname { get; set; }
public SimContactDetails2G()
{
}
public SimContactDetails2G(String name, String phoneNumber)
{
AbreviatedName = name;
DialingNumber = phoneNumber;
}
}
i want to create a list of the above objects. so i do,
List<SimContactDetails2G> simContactDetails2GToBackUp = new List<SimContactDetails2G>();
However, when I try to retrieve the count of items on the variable "simContactDetails2GToBackUp
", it gives the folwing error:
Count =
'((System.Collections.Generic.List&l开发者_高级运维t;T>)(simContactDetails2GToBackUp)).Count'
threw an exception of type
'System.ArgumentException'
the program does not crash, but I cant access the count property. I can add items on the list, the list is populated. But I can't get the count. Any idea for this strange behaviour?
Why are you casting to List<T>
instead of List<SimContactDetails2G>
?
use it like:
int Count = simContactDetails2GToBackUp.Count; //you can use it without any typecast
or
int Count = ((System.Collections.Generic.List<SimContactDetails2G>)(simContactDetails2GToBackUp)).Count;//you will have to specify its type while performing typecast.
i have tested it, its working with above code. but first one is preferable. because there is not need to typecast.
精彩评论