Web Development

Iterators and Collections implementation in C#

Here the example of Iterators and Collections in combine. Here, DictionaryBase is used in Collection. Lets implement it.

// Program: Collections and Iterations
// Purpose: Training period
// By: H.M. Saiful Islam

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

namespace CollectionIterators
{
    class Program
    {
        static void Main(string[] args)
        {
            Animals animalCollection = new Animals();
            animalCollection.Add( "0",new Cow ("Jack"));

            
            Console.ReadKey();

            foreach (Animal mm in animalCollection)
            {
                Console.WriteLine("New {0} object added to custom collection, name=", mm.Name);
            }
            Console.ReadKey();
        }

        
    }

    

    public class Animals : DictionaryBase
    {
        public void Add(string newId, Animal newAnimal)
        {
            Dictionary.Add(newId, newAnimal);
        }

        public void Remove(string animalID)
        {
            Dictionary.Remove(animalID);
        }

        public Animals()
        {
        }

        public Animal this[string animalID]
        {
            get
            {
                return (Animal)Dictionary[animalID];
            }
            set
            {
                Dictionary[animalID] = value;
            }
        }

        public new IEnumerator GetEnumerator()
        {
            foreach (object animal in Dictionary.Values)
                yield return (Animal)animal;
        }
    }

    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 Chicken : Animal
    {
        public void LayEgg()
        {
            Console.WriteLine("{0} has laid an egg.", name);
        }
        public Chicken(string newName)
            : base(newName)
        {
        }
    }

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

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

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