網(wǎng)站首頁 編程語言 正文
一、非泛型方式,繼承自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<T>
下面的代碼示例演示如何從構(gòu)造類型的派生集合類Collection<T>泛型類,以及如何重寫受保護(hù)InsertItem, RemoveItem, ClearItems,和SetItem方法,以提供自定義行為Add, Insert, Remove,和Clear方法,并設(shè)置Item[Int32]屬性。
此示例中提供的自定義行為是Changed每個受保護(hù)方法結(jié)束時引發(fā)的通知事件。
Dinosaurs類繼承Collection<string>,并定義Changed事件,使用DinosaursChangedEventArgs類用于事件信息和使用枚舉標(biāo)識的更改種類。
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
public class Dinosaurs : Collection<string>
{
public event EventHandler<DinosaursChangedEventArgs> Changed;
protected override void InsertItem(int index, string newItem)
{
base.InsertItem(index, newItem);
EventHandler<DinosaursChangedEventArgs> 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<DinosaursChangedEventArgs> 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<DinosaursChangedEventArgs> temp = Changed;
if (temp != null)
{
temp(this, new DinosaursChangedEventArgs(
ChangeType.Removed, removedItem, null));
}
}
protected override void ClearItems()
{
base.ClearItems();
EventHandler<DinosaursChangedEventArgs> 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<string> 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);
}
}
}
原文鏈接:https://www.cnblogs.com/springsnow/archive/2011/06/10/2077553.html
相關(guān)推薦
- 2023-03-16 iOS?schem與Universal?Link?調(diào)試時踩坑解決記錄_IOS
- 2022-06-06 typescript中abstractClass(抽象類)、extends、abstract
- 2022-07-13 求兩個降序單鏈表的并集(利用原有鏈點(diǎn))
- 2022-02-20 Android?WebView實(shí)現(xiàn)全屏播放視頻_Android
- 2024-03-24 MyBatis-Plus QueryWrapper及LambdaQueryWrapper的使用
- 2022-08-01 C++簡單又好用的基本運(yùn)算符重載_C 語言
- 2023-03-20 C#?BeginInvoke實(shí)現(xiàn)異步編程方式_C#教程
- 2022-07-26 Android自定義評分控件的完整實(shí)例_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支