信息
您所需的主题如下所示。但此主题未包含在此库中。

Collection<T>.SetItem 方法

2013/12/13

替换指定索引处的元素。

Namespace:  System.Collections.ObjectModel
程序集:  mscorlib(位于 mscorlib.dll 中)

protected virtual void SetItem(
	int index,
	T item
)

参数

index
类型: System.Int32
待替换元素的从零开始的索引。
item
类型: T
位于指定索引处的元素的新值。对于引用类型,该值可以为 null

异常条件
ArgumentOutOfRangeException

index 小于零。

- 或 -

index 大于 Count

Collection<T> 接受 null 作为引用类型的有效值并且允许有重复的元素。

此方法是 O(1) 操作。

下面的代码示例演示如何从 Collection<T> 泛型类的构造类型派生集合类,以及如何重写受保护的 InsertItemRemoveItemClearItemsSetItem 方法以提供 AddInsertRemoveClear 方法以及设置 Item 属性的自定义行为。

此示例提供的自定义行为是 Changed 通知事件,该事件在每个受保护的方法末尾引发。Dinosaurs 类继承 Collection<string>(在 Visual Basic 中为 Collection(Of String)),并定义 Changed 事件,该事件使用 DinosaursChangedEventArgs 类获取事件信息,使用枚举标识更改的类型。

此代码示例调用 Collection<T> 的若干属性和方法以阐释自定义事件。


using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Controls;

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 Example
{

    private static TextBlock _textblock;


    public static void Demo(TextBlock outputBlock)
    {

        Example._textblock = outputBlock;

        Dinosaurs dinosaurs = new Dinosaurs();

        dinosaurs.Changed += ChangedHandler;

        dinosaurs.Add("Psitticosaurus");
        dinosaurs.Add("Caudipteryx");
        dinosaurs.Add("Compsognathus");
        dinosaurs.Add("Muttaburrasaurus");

        Display(dinosaurs);

        outputBlock.Text += String.Format("\nIndexOf(\"Muttaburrasaurus\"): {0}\n",
            dinosaurs.IndexOf("Muttaburrasaurus"));

        outputBlock.Text += String.Format("\nContains(\"Caudipteryx\"): {0}\n",
            dinosaurs.Contains("Caudipteryx"));

        outputBlock.Text += String.Format("\nInsert(2, \"Nanotyrannus\")\n");
        dinosaurs.Insert(2, "Nanotyrannus");

        outputBlock.Text += String.Format("\ndinosaurs[2]: {0}\n", dinosaurs[2]);

        outputBlock.Text += "\ndinosaurs[2] = \"Microraptor\"" + "\n";
        dinosaurs[2] = "Microraptor";

        outputBlock.Text += "\nRemove(\"Microraptor\")\n";
        dinosaurs.Remove("Microraptor");

        outputBlock.Text += "\nRemoveAt(0)\n";
        dinosaurs.RemoveAt(0);

        Display(dinosaurs);
    }

    private static void Display(Collection<string> cs)
    {
        _textblock.Text += "\n";
        foreach (string item in cs)
        {
            _textblock.Text += item + "\n";
        }
    }

    private static void ChangedHandler(object source,
        DinosaursChangedEventArgs e)
    {

        if (e.ChangeType == ChangeType.Replaced)
        {
            _textblock.Text += String.Format("{0} was replaced with {1}\n", e.ChangedItem,
                e.ReplacedWith);
        }
        else if (e.ChangeType == ChangeType.Cleared)
        {
            _textblock.Text += "The dinosaur list was cleared." + "\n";
        }
        else
        {
            _textblock.Text += String.Format("{0} was {1}.\n", e.ChangedItem, e.ChangeType);
        }
    }
}


/* This code example produces the following output:

Psitticosaurus was Added.
Caudipteryx was Added.
Compsognathus was Added.
Muttaburrasaurus was Added.

Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus

IndexOf("Muttaburrasaurus"): 3

Contains("Caudipteryx"): True

Insert(2, "Nanotyrannus")
Nanotyrannus was Added.

dinosaurs[2]: Nanotyrannus

dinosaurs[2] = "Microraptor"
Nanotyrannus was replaced with Microraptor

Remove("Microraptor")
Microraptor was Removed.

RemoveAt(0)
Psitticosaurus was Removed.

Caudipteryx
Compsognathus
Muttaburrasaurus
 */


Windows Phone OS

受以下版本支持: 8.0, 7.1, 7.0

Windows Phone

显示: