A general List.Count list provides 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 get the number of elements in the variable " simContactDetails2GToBackUp", this gives a folwing error:

Count = '((System.Collections.Generic.List<T>)(simContactDetails2GToBackUp)).Count' threw an exception of type 'System.ArgumentException'

The program does not crash, but I cannot access the count property. I can add items to the list, the list is populated. But I can’t get the bill. Any idea for this weird behavior?

+3
source share
2 answers

Why did you choose List<T>instead List<SimContactDetails2G>?

0
source

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.

, . . .

0

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


All Articles