tags:

views:

79

answers:

2

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<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?

A: 

Why are you casting to List<T> instead of List<SimContactDetails2G>?

Sebastian Piu
i am not casting to List<T>. its the error that i have posted exactly as i get it when debugging.
NeerajC
A: 

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.

Rajesh Rolen- DotNet Developer