How to determine the number of unique derived types in an array?

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 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 to find out how many objects are in the array NormalCustomersand how many objects are in the array SubscriberCustomers?

+4
source share
4 answers

You can use OfType

customers.OfType<NormalCustomer>().Count() 

You will need to import the namespace System.Linqusing the using directive:

using System.Linq;
+8
source

I agree with Alexei to answer, but if you want an alternative:

// 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);
+2
source

LINQ :

var groupedCustomers = customers.GroupBy(c => c.GetType()).ToList();

foreach(var customer in groupedCustomers)
{
   Console.WriteLine(customer.Key.Name + ", count: " + customer.Count());
}

https://ideone.com/fz93H6

, System.Linq :

using System.Linq;
0

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            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();

            int normalCount = 0;
            int subscriberCount = 0;

            foreach (Customer customer in customers)
            {
                if (customer != null)
                {
                    string customerName = customer.GetType().ToString();
                    // get name without namespace
                    customerName = customerName.Substring(customerName.LastIndexOf(".") + 1);
                    switch (customerName)
                    {
                        case "NormalCustomer":
                            normalCount++;
                            break;
                        case "SubscriberCustomer":
                            subscriberCount++;
                            break;
                    }
                }

            }
        }
    }
    abstract class Customer
    {
        public int id;
        public string name;
        public double balance;
    }
    class NormalCustomer : Customer
    {
    }
    class SubscriberCustomer : Customer
    {
        public int LandMinutes;
        public int MobileMinutes;
    }
}
0

All Articles