最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#集合之自定義集合類

 更新時間:2022年05月05日 10:19:12   作者:springsnow  
這篇文章介紹了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<T>

下面的代碼示例演示如何從構造類型的派生集合類Collection<T>泛型類,以及如何重寫受保護InsertItem, RemoveItem, ClearItems,和SetItem方法,以提供自定義行為Add, Insert, Remove,和Clear方法,并設置Item[Int32]屬性。
此示例中提供的自定義行為是Changed每個受保護方法結束時引發(fā)的通知事件。
Dinosaurs類繼承Collection<string>,并定義Changed事件,使用DinosaursChangedEventArgs類用于事件信息和使用枚舉標識的更改種類。

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);
        }
    }
}

到此這篇關于C#集合之自定義集合類的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

漳平市| 焦作市| 仁化县| 平谷区| 河曲县| 辽阳县| 宁武县| 临夏县| 长寿区| 宜昌市| 泰宁县| 徐汇区| 兴隆县| 阆中市| 商河县| 金湖县| 三河市| 乐昌市| 景谷| 海南省| 繁昌县| 阜康市| 沈丘县| 永济市| 亚东县| 永定县| 福泉市| 林甸县| 金坛市| 祁连县| 嘉善县| 札达县| 右玉县| 奉化市| 山丹县| 德清县| 东阿县| 桂林市| 吴堡县| 尚志市| 钦州市|