KeyedCollection<TKey, TItem> 可以自定义集合 O(1) 可自定义键值

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.ObjectModel;
namespace KeyedCollectionDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            SimpleOrder my = new SimpleOrder();
            my.Add(new OrderItem(1001,"hello",1,1));
            my.Add(new OrderItem(1002, "hells", 1, 1));
            my.Add(new OrderItem(1003, "hellss", 1, 1));
            Console.WriteLine(my[1002].ToString());
            foreach (OrderItem m in my)
            {
              Console.WriteLine(m.ToString());
            }
            Console.ReadKey();
        }
    }

    public class SimpleOrder : KeyedCollection<int, OrderItem>
    {
        public SimpleOrder()
            : base()
        { }
        protected override int GetKeyForItem(OrderItem item)
        {
            return item.PartNumber;
        }

    }
    public class OrderItem
    {
        public readonly int PartNumber;
        public readonly string Description;
        public readonly double UnitPrice;
        private int _quantity = 0;
        public OrderItem(int partNumber, string description, int quantity, double unitPrice)
        {
            this.PartNumber = partNumber;
            this.Description = description;
            this.Quantity = quantity;
            this._quantity = quantity;
        }
        public int Quantity
        {
            get { return _quantity; }
            set
            {
                if (value < 0)
                {
                    throw new ArgumentException("Quantity cannot be negative.");
                }
                else
                {
                    _quantity = value;
                }
            }
        }
        public override string ToString()
        {
            return String.Format(
                "{0,9} {1,6} {2,-12} at {3,8:#,###.00} = {4,10:###,###.00}",
                PartNumber, _quantity, Description, UnitPrice,
                UnitPrice * _quantity);
        }
    }
}

KeyedCollection<TKey, TItem> 类同时提供了 O(1) 索引检索和到达 O(1) 的键控检索。它是一个抽象类型,更确切地说,是一组抽象类型的无穷集,因为它的每个构造的泛型类型都是一个抽象基类。若要使用KeyedCollection<TKey, TItem>,请从适当的构造类型中派生您的集合类型。

KeyedCollection<TKey, TItem> 类是基于 IList<T> 泛型接口的集合与基于 IDictionary<TKey, TValue> 泛型接口的集合之间的组合体。与基于 IList<T> 泛型接口的集合一样,KeyedCollection<TKey, TItem> 也是项的索引列表。与基于 IDictionary<TKey, TValue> 泛型接口的集合一样,KeyedCollection<TKey, TItem> 包含与每个元素关联的一个键。

与字典不同,KeyedCollection<TKey, TItem> 的元素不是键/值对;相反,整个元素是值,而键嵌入在值内。例如,从 KeyedCollection<String,String>(在 Visual Basic 中为 KeyedCollection(Of String, String))派生的集合的元素可以是“John Doe Jr.”,其中值为“John Doe Jr.”,键为“Doe”;也可以从 KeyedCollection<int,Employee> 派生包含整数键的雇员记录的集合。抽象 GetKeyForItem 方法从元素中提取键。

默认情况下,KeyedCollection<TKey, TItem> 包括一个您可以使用 Dictionary 属性获得的查找字典。当将某一项添加到 KeyedCollection<TKey, TItem> 时,该项的关键字会被一次性提取并保存到查找字典中以加快搜索速度。通过在创建 KeyedCollection<TKey, TItem> 时指定字典创建阈值可重写此行为。元素数首次超过该阈值时将创建查找字典。如果指定的阈值为 -1,则在任何情况下都不会创建查找字典。

posted on 2012-12-03 10:16  R.Ray  阅读(348)  评论(0)    收藏  举报

导航