extends DictionaryBase : DictionaryBase « Collections Data Structure « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Collections Data Structure » DictionaryBaseScreenshots 
extends DictionaryBase
  

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

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 milked.", name);
    }

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

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 new IEnumerator GetEnumerator() {
        foreach (object animal in Dictionary.Values)
            yield return (Animal)animal;
    }

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

class Program {
    static void Main(string[] args) {
        Animals animalCollection = new Animals();
        animalCollection.Add("A"new Cow("A"));
        animalCollection.Add("B"new Chicken("B"));

        foreach (Animal myAnimal in animalCollection) {
            Console.WriteLine(myAnimal.ToString() + myAnimal.Name);
        }

    }
}

   
  
Related examples in the same category
1.Add a list of items into the dictionary.
2.Copy/Clone all the element from a dictionary to another.
3.Put an element into the Dictionary
4.Add one dictionary items into another. Copy all the first dictionary's items into the second dictionary.
5.A dictionary with keys of type string and values of type String
6.A dictionary with keys of type string and values of type Type
7.Dictionary to anonymous type
8.Two Key Dictionary
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.