Pages

Wednesday, April 17, 2013

Generic Collections

A generic collection is strongly typed (type safe), meaning that you can only put one type of object into it. This eliminates type mismatches at runtime. Another benefit of type safety is that performance is better with value type objects because they don't incur overhead of being converted to and from type object.
Creating Generic List<T> Collections
Ex:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lists
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>();
            list.Add(1);
            list.Add(2);
            list.Add(3);
            list.Add(4);
            foreach (var i in list)
            {
                Console.WriteLine(i);
                Console.WriteLine("\n");
            }
            Console.ReadKey();
        }
    }
}
The first thing you should notice is the generic collection List<int>, which is referred to as List of int. If you looked in the documentation for this class, you would find that it is defined as List<T>, where T could be any type. For example, if you wanted the list to work on string or Customer objects, you could define them as List<string> or List<Customer> and they would hold only string or Customer objects

Dictionary<TKey, TValue> Collections
Another very useful generic collection is the Dictionary, which works with key/value pairs. There is a non-generic collection, called a Hashtable that does the same thing, except that it operates on type object.

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

namespace Dictionary
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<int, Customer> customers = new Dictionary<int, Customer>();
            Customer customer1 = new Customer(1, "customer1");
            Customer customer2 = new Customer(2, "customer2");
            Customer customer3 = new Customer(3, "customer3");
            customers.Add(customer1.customerID, customer1);
            customers.Add(customer2.customerID, customer2);
            customers.Add(customer3.customerID, customer3);

            foreach (KeyValuePair<int, Customer> cust in customers)
            {
                Console.WriteLine("CustomerID-{0} and CustomerName-{1}\n", cust.Key, cust.Value.customerName);
            }
            Console.ReadKey();
        }
    }
    class Customer
    {
        public Customer(int custID, string Name)
        {
            customerID = custID;
            customerName = Name;
        }
        public int customerID
        {
            get;
            set;
        }
        public string customerName
        {
            get;
            set;
        }
    }
}
Whenever coding with the generic collections, add a using System.Collections.Generic declaration to your file.There are many more generic collections to choose from also, such as Stack, Queue, and SortedDictionary.

0 comments:

Post a Comment