I have the following classes
Customer
Class
abstract class Customer
{
public int id;
public string name;
public double balance;
}
class NormalCustomer
class NormalCustomer: Customer
{
}
class SubscriberCustomer
class SubscriberCustomer:Customer
{
public int LandMinutes;
public int MobileMinutes;
}
If we create an array of Customers
Customer[] customers = new Customer[100];
customers[0]=new NormalCustomer();
customers[1] = new NormalCustomer();
customers[2] = new SubscriberCustomer();
customers[3] = new NormalCustomer();
customers[4] = new SubscriberCustomer();
The question is how do I know how many object in the array are NormalCustomers
and how many object in the array are SubscriberCustomers
?
I agree with Aleksey's answer - but if you want alternatives:
// Result is a sequence of Type/Count objects
var groupedByActualType = customers.GroupBy(
x => x.GetType(), (type, values) => new { Type = type, Count = values.Count() });
Or:
var normal = customers.Count(c => c is NormalCustomer);
var subscribers = customers.Count(c => c is SubscriberCustomer);
See more on this question at Stackoverflow