(2010-08-02) C#.NET HashTable And SortList

摘要:(2010-08-02) C#.NET HashTable And SortList

HashTable 實作 IDictionary

SortList  實作 IDictionary  實作 IList  實作 ICollection

類別1

 

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

namespace mod02
{
    class Employee:IComparable
    {
        private String _id;

        public String Id
        {
            get { return _id; }
            set { _id = value; }
        }
        private String _name;

        public String Name
        {
            get { return _name; }
            set { _name = value; }
        }

        #region IComparable<Employee> 成員

        public int CompareTo(Object other)
        {
            return this.Id.CompareTo(((Employee)other).Id);
        }

        #endregion
    }
}

類別2

 

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

namespace mod02
{
    class Car:IComparable
    {
        private String _brand;

        public String Brand
        {
            get { return _brand; }
            set { _brand = value; }
        }

        #region IComparable 成員

        public int CompareTo(object obj)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}

主程式 HashTable

 

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

namespace mod02
{
    class TestHashAndSortList
    {
        public static void Main()
        {
            //雜湊沒有順序性
            System.Collections.Hashtable hash = new System.Collections.Hashtable();
            hash.Add(new Employee() { Id = "0001", Name = "eric" },
                new Car() { Brand = "福斯" });
            hash.Add(new Employee() { Id = "0002", Name = "linda" },
             new Car() { Brand = "日產" });
            hash.Add(new Employee() { Id = "0003", Name = "sam" },
             new Car() { Brand = "兵土" });
            //取出所有Key
            System.Collections.ICollection keys = hash.Keys;
            System.Collections.IEnumerator em = keys.GetEnumerator();
            while (em.MoveNext())
            {
                Employee emp = (Employee)em.Current;
                //問汽車
                Car car = (Car)hash[emp];
                System.Console.WriteLine(String.Format("員工:{0}  汽車:{1}", emp.Name, car.Brand));
            }


        }
    }
}

 

主程式 SorList

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

namespace mod02
{
    class TestSortedList
    {
        public static void Main()
        {
            //雜湊沒有順序性
            System.Collections.SortedList list = new System.Collections.SortedList();
            list.Add(new Employee() { Id = "0005", Name = "eric" },
                new Car() { Brand = "福斯" });
            list.Add(new Employee() { Id = "0002", Name = "linda" },
             new Car() { Brand = "日產" });
            list.Add(new Employee() { Id = "0001", Name = "allen" },
             new Car() { Brand = "兵土" });
            System.Collections.ICollection col = list.Keys;
            System.Collections.IEnumerator em = col.GetEnumerator();
            while (em.MoveNext())
            {
                Employee emp = (Employee)em.Current;
                Car car = (Car)list[emp];
                System.Console.WriteLine(String.Format("員工:{0}  汽車:{1}", emp.Id, car.Brand));
            }
        }
    }
}