Collections in C#

Collections: Collection enable you to maintain groups of objects. Collection classes in general are used for maintaining lists of objects and may expose more functionality than simple arrays. Much of this functionality comes through implementing interfaces from the System.Collections namespace.

There are a number of interfaces in the System.Collections namespaces that provide basic collection functionally:

  • IEnumerable : Provides the capability to loop through items in a collection.
  • ICollection : Provides the ability to obtain the number of items in a collection and to copy items into a simple array type (inherits from IEnumerable)
  • IList : Provides a list of items for a collection along with the capabilities for accessing theseitems, and some other basic capabilities related to lists of items.
  • IDictionary: Similar to IList, but provides a list of items accessible via a key value rather than an index.

Here the example of Collections:

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

namespace Collections
{
    public abstract class Animal
    {
        protected string name;

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public Animal()
        {
            name = "The Animal with no name";
        }

        public Animal(string NewName)
        {
            name = NewName;
        }

        public void Feed()
        {
            Console.WriteLine("{0} has been fed.", name);
        }
    }

    public class Cow : Animal
    {
        public void Milk()
        {
            Console.WriteLine("{0} has been fed.", name);
        }

        public Cow(string newName)
            : base(newName)
        {

        }
    }

    public class Chicken : Animal
    {
        public void LayEgg()
        {
            Console.WriteLine("{0} has laid an egg.", name);
        }
        public Chicken(string newName)
            : base(newName)
        {
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Create an Array type collection of Animal objects and use it:");
            Animal[] animalArray = new Animal[2];
            Cow myCow1 = new Cow("Dierdre");
            animalArray[0] = myCow1;
            animalArray[1] = new Chicken("Ken");
            foreach (Animal myAnimal1 in animalArray)
            {
                Console.WriteLine("New {0} object added to Array collection, Name={1}", myAnimal1.ToString(),
                    myAnimal1.Name);

            }
            Console.WriteLine("Array collection contains {0} objects.", animalArray.Length);
            animalArray[0].Feed();
            ((Chicken)animalArray[1]).LayEgg();
            Console.ReadKey();

        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s