C#集合之自定义集合类
一、非泛型方式,继承自CollectionBase
public class MyClass{public static void Main(){StringCollection myStringCol = new StringCollection(); myStringCol.Add("a"); myStringCol.Add("b"); Console.Write(myStringCol[0]); foreach (string myAnimal in myStringCol){Console.Write(myAnimal); }Console.ReadKey(); }}//自定义集合类public class StringCollection : CollectionBase{public void Add(string newAnimal){List.Add(newAnimal); }public void Remove(string newAnimal){List.Remove(newAnimal); }public StringCollection() { }public string this[int animalIndex]{get { return (string)List[animalIndex]; }set { List[animalIndex] = value; }}}
二、泛型方式,继承自Collection
此示例中提供的自定义行为是Changed每个受保护方法结束时引发的通知事件。
Dinosaurs类继承Collection,并定义Changed事件,使用DinosaursChangedEventArgs类用于事件信息和使用枚举标识的更改种类。
using System; using System.Collections.Generic; using System.Collections.ObjectModel; public class Dinosaurs : Collection{public event EventHandlerChanged; protected override void InsertItem(int index, string newItem){base.InsertItem(index, newItem); EventHandler temp = Changed; if (temp != null){temp(this, new DinosaursChangedEventArgs(ChangeType.Added, newItem, null)); }}protected override void SetItem(int index, string newItem){string replaced = Items[index]; base.SetItem(index, newItem); EventHandler temp = Changed; if (temp != null){temp(this, new DinosaursChangedEventArgs(ChangeType.Replaced, replaced, newItem)); }}protected override void RemoveItem(int index){string removedItem = Items[index]; base.RemoveItem(index); EventHandler temp = Changed; if (temp != null){temp(this, new DinosaursChangedEventArgs(ChangeType.Removed, removedItem, null)); }}protected override void ClearItems(){base.ClearItems(); EventHandler temp = Changed; if (temp != null){temp(this, new DinosaursChangedEventArgs(ChangeType.Cleared, null, null)); }}}// Event argument for the Changed event.//public class DinosaursChangedEventArgs : EventArgs{public readonly string ChangedItem; public readonly ChangeType ChangeType; public readonly string ReplacedWith; public DinosaursChangedEventArgs(ChangeType change, string item, string replacement){ChangeType = change; ChangedItem = item; ReplacedWith = replacement; }}public enum ChangeType{Added, Removed, Replaced, Cleared}; public class Demo{public static void Main(){Dinosaurs dinosaurs = new Dinosaurs(); dinosaurs.Changed += ChangedHandler; dinosaurs.Add("Psitticosaurus"); dinosaurs.Add("Caudipteryx"); dinosaurs.Add("Compsognathus"); dinosaurs.Add("Muttaburrasaurus"); Display(dinosaurs); Console.WriteLine("\nIndexOf(\"Muttaburrasaurus\"): {0}", dinosaurs.IndexOf("Muttaburrasaurus")); Console.WriteLine("\nContains(\"Caudipteryx\"): {0}", dinosaurs.Contains("Caudipteryx")); Console.WriteLine("\nInsert(2, \"Nanotyrannus\")"); dinosaurs.Insert(2, "Nanotyrannus"); Console.WriteLine("\ndinosaurs[2]: {0}", dinosaurs[2]); Console.WriteLine("\ndinosaurs[2] = \"Microraptor\""); dinosaurs[2] = "Microraptor"; Console.WriteLine("\nRemove(\"Microraptor\")"); dinosaurs.Remove("Microraptor"); Console.WriteLine("\nRemoveAt(0)"); dinosaurs.RemoveAt(0); Display(dinosaurs); }private static void Display(Collection cs){Console.WriteLine(); foreach( string item in cs ){Console.WriteLine(item); }}private static void ChangedHandler(object source, DinosaursChangedEventArgs e){if (e.ChangeType==ChangeType.Replaced){Console.WriteLine("{0} was replaced with {1}", e.ChangedItem, e.ReplacedWith); }else if(e.ChangeType==ChangeType.Cleared){Console.WriteLine("The dinosaur list was cleared."); }else{Console.WriteLine("{0} was {1}.", e.ChangedItem, e.ChangeType); }}}
到此这篇关于C#集合之自定义集合类的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
推荐阅读
- C#入门学习之集合、比较和转换
- Trapping Rain Water
- h5跳转到app的实现
- Android_(传感器)获取手机中的传感器
- 进阶之路|大聪明教你学Java | 面试官(请你说说 Redis 为什么这么快())
- Android List,Set,Map集合安全 集合区别 并发集合类性能分析
- 在 app 界面布局和静态页面编写之前需要了解的事
- python|Python数据可视化大杀器之地阶技法(matplotlib(含详细代码))
- App自动化测试必备之adb使用
- Gradle之Android Gradle Plugin 主要 Task 分析