亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#集合之自定義集合類

 更新時(shí)間:2022年05月05日 10:19:12   作者:springsnow  
這篇文章介紹了C#集合之自定義集合類,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、非泛型方式,繼承自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每個(gè)受保護(hù)方法結(jié)束時(shí)引發(fā)的通知事件。
Dinosaurs類繼承Collection<string>,并定義Changed事件,使用DinosaursChangedEventArgs類用于事件信息和使用枚舉標(biāo)識(shí)的更改種類。

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#集合之自定義集合類的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • c#編寫的高并發(fā)數(shù)據(jù)庫控制訪問代碼

    c#編寫的高并發(fā)數(shù)據(jù)庫控制訪問代碼

    往往大數(shù)據(jù)量,高并發(fā)時(shí), 瓶頸都在數(shù)據(jù)庫上, 好多人都說用數(shù)據(jù)庫的復(fù)制,發(fā)布, 讀寫分離等技術(shù), 但主從數(shù)據(jù)庫之間同步時(shí)間有延遲.
    2015-03-03
  • 一篇文章教會(huì)你用Unity制作網(wǎng)格地圖生成組件

    一篇文章教會(huì)你用Unity制作網(wǎng)格地圖生成組件

    網(wǎng)格地圖這個(gè)功能在策略型游戲中應(yīng)用比較廣泛,基本情況下會(huì)將地圖分割成正方形網(wǎng)格或者六邊形網(wǎng)格,這篇文章主要給大家介紹了如何通過一篇文章學(xué)會(huì)用Unity制作網(wǎng)格地圖生成組件的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • c# DataDirectory的用法

    c# DataDirectory的用法

    這篇文章主要介紹了c# DataDirectory的用法,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下
    2020-08-08
  • c# 匿名方法的小例子

    c# 匿名方法的小例子

    c# 匿名方法的小例子,需要的朋友可以參考一下
    2013-04-04
  • C#實(shí)現(xiàn)打字游戲

    C#實(shí)現(xiàn)打字游戲

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)打字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • 詳解C# 枚舉高級(jí)用法之Description

    詳解C# 枚舉高級(jí)用法之Description

    這篇文章主要介紹了C# 枚舉高級(jí)用法之Description,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • C# pictureBox用法案例詳解

    C# pictureBox用法案例詳解

    這篇文章主要介紹了C# pictureBox用法案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • c#協(xié)變和逆變實(shí)例分析

    c#協(xié)變和逆變實(shí)例分析

    這篇文章主要介紹了c#協(xié)變和逆變,以實(shí)例形式詳細(xì)講述了協(xié)變和逆變的原理與具體用法,具有一定的學(xué)習(xí)借鑒價(jià)值,需要的朋友可以參考下
    2014-10-10
  • C#?Math中常用數(shù)學(xué)運(yùn)算的示例詳解

    C#?Math中常用數(shù)學(xué)運(yùn)算的示例詳解

    Math?為通用數(shù)學(xué)函數(shù)、對(duì)數(shù)函數(shù)、三角函數(shù)等提供常數(shù)和靜態(tài)方法,使用起來非常方便。這篇文章主要為大家介紹幾個(gè)常用的數(shù)學(xué)運(yùn)算的使用,需要的可以參考一下
    2022-11-11
  • C#對(duì)接阿里云IOT平臺(tái)進(jìn)行設(shè)備開發(fā)

    C#對(duì)接阿里云IOT平臺(tái)進(jìn)行設(shè)備開發(fā)

    這篇文章介紹了C#對(duì)接阿里云IOT平臺(tái)進(jìn)行設(shè)備開發(fā),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-01-01

最新評(píng)論