日期:2014-05-17 浏览次数:21039 次
using System.Collections.Generic; public class MyClassCollection : IEnumerable<MyClass> { private List<MyClass> _Items = new List<MyClass>(); private Dictionary<object, int> _Key = new Dictionary<object, int>(); public int IndexOf(MyClass item) { return _Items.IndexOf(item); } public bool Contains(string key) { return _Key.ContainsKey(key); } public bool Contains(MyClass item) { return _Items.Contains(item); } public MyClass this[int i] { get { if (i >= 0 && i < Count) { return _Items[i]; } else { return null; } } } public MyClass this[object key] { get { if (_Key.ContainsKey(key)) { return _Items[_Key[key]]; } else { return null; } } } public string CommandString { get; set; } public bool Add(MyClass item) { if (_Items.Contains(item)) return false; _Key.Add(item.Key.Trim().ToLower(), _Items.Count); _Items.Add(item); return true; } public bool Remove(object key) { if (_Key.ContainsKey(key) == false) return false; return this.RemoveAt(_Key[key]); } public bool Remove(MyClass item) { if (_Items.Contains(item) == false) return false; return this.RemoveAt(_Items.IndexOf(item)); } public bool RemoveAt(int i) { if (i < 0 || i > _Items.Count - 1) return false; _Key.Remove(_Items[i].Key); _Items.RemoveAt(i); if (i < _Items.Count) { for (int j = i; j <= _Items.Count - 1; j++) { _Key[_Items[j].Key.Trim().ToLower()] = j; } } return true; } public void Clear() { _Items.Clear(); _Key.Clear(); } public int Count { get { return _Items.Count; } } public System.Collections.Generic.IEnumerator<MyClass> GetEnumerator() { return _Items.GetEnumerator(); } public System.Collections.IEnumerator GetEnumerator1() { return GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator1(); } } public class MyClass { public string Key { get; set; } public string Value1 { get; set; } public string Value2 { get; set; } public string Value3{ get; set; } }
------解决方案--------------------
HashTable、Dictionary,
.net3.5还有HashSet这种。