C#集合之自定義集合類(lèi)
一、非泛型方式,繼承自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();
}
}
//自定義集合類(lèi)
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)造類(lèi)型的派生集合類(lèi)Collection<T>泛型類(lèi),以及如何重寫(xiě)受保護(hù)InsertItem, RemoveItem, ClearItems,和SetItem方法,以提供自定義行為Add, Insert, Remove,和Clear方法,并設(shè)置Item[Int32]屬性。
此示例中提供的自定義行為是Changed每個(gè)受保護(hù)方法結(jié)束時(shí)引發(fā)的通知事件。
Dinosaurs類(lèi)繼承Collection<string>,并定義Changed事件,使用DinosaursChangedEventArgs類(lèi)用于事件信息和使用枚舉標(biāo)識(shí)的更改種類(lèi)。
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);
}
}
}到此這篇關(guān)于C#集合之自定義集合類(lèi)的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#難點(diǎn)逐個(gè)擊破(5):類(lèi)的訪(fǎng)問(wèn)類(lèi)型
類(lèi)的訪(fǎng)問(wèn)類(lèi)型有時(shí)也叫訪(fǎng)問(wèn)級(jí)別,使用以下訪(fǎng)問(wèn)修改符:Public、Protected、Private、internal、protected internal。2010-02-02
Unity ScrollView實(shí)現(xiàn)動(dòng)態(tài)列表生成
這篇文章主要為大家詳細(xì)介紹了Unity ScrollView實(shí)現(xiàn)動(dòng)態(tài)列表生成,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
Unity中的PostProcessScene實(shí)用案例深入解析
這篇文章主要為大家介紹了Unity中的PostProcessScene實(shí)用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
C#導(dǎo)出數(shù)據(jù)到Excel文件的方法
這篇文章主要介紹了C#導(dǎo)出數(shù)據(jù)到Excel文件的方法,涉及C#操作Excel的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
C#實(shí)現(xiàn)讀取被進(jìn)程占用的文件實(shí)現(xiàn)方法
這篇文章主要介紹了C#實(shí)現(xiàn)讀取被進(jìn)程占用的文件實(shí)現(xiàn)方法,涉及C#進(jìn)程操作及文件讀取的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
C# 通過(guò)反射獲取類(lèi)型的字段值及給字段賦值的操作
這篇文章主要介紹了C# 通過(guò)反射獲取類(lèi)型的字段值及給字段賦值的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
c#求范圍內(nèi)素?cái)?shù)的示例分享(c#求素?cái)?shù))
問(wèn)題是判斷101-200之間有多少個(gè)素?cái)?shù),并輸出所有素?cái)?shù)。下面是使用C#解決這個(gè)問(wèn)題的方法 ,需要的朋友可以參考下2014-03-03
C#實(shí)現(xiàn)一個(gè)控制臺(tái)的點(diǎn)餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)一個(gè)控制臺(tái)的點(diǎn)餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11

