Collection.RemoveItem メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Collection.RemoveItem メソッドの意味・解説 

Collection.RemoveItem メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

Collection指定したインデックスにある要素削除します

名前空間: System.Collections.ObjectModel
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Protected Overridable Sub
 RemoveItem ( _
    index As Integer _
)
protected virtual void RemoveItem (
    int index
)
protected:
virtual void RemoveItem (
    int index
)
protected void RemoveItem (
    int index
)

パラメータ

index

削除する要素の、0 から始まるインデックス番号

例外例外
例外種類条件

ArgumentOutOfRangeException

index が 0 未満です。

または

indexCount上です。

解説解説

このメソッドは O(n) 操作です。ここで、nCount です。

使用例使用例

このコード例では、Collection ジェネリック クラス構築型からコレクション クラス派生させる方法、および InsertItem、RemoveItem、ClearItems、および SetItem の各プロテクト メソッドオーバーライドして AddInsertRemove、および Clear の各メソッドカスタム動作や、Item プロパティ設定するためのカスタム動作提供する方法示します

この例で提供されるカスタム動作は、Changed という名前の通知イベントです。これは、各プロテクト メソッド最後発生しますDinosaurs クラスは、Collection<string> (Visual Basic の場合Collection(Of String)) を継承しChanged イベント定義しますイベント情報のために DinosaursChangedEventArgs クラス使用され変更種類識別するために列挙体が使用されます。

次のコード例では、Collectionいくつかのプロパティおよびメソッド呼び出してカスタム イベントの例を示します

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Public Class Dinosaurs
    Inherits Collection(Of String)

    Public Event Changed As
 EventHandler(Of DinosaursChangedEventArgs)

    Protected Overrides Sub
 InsertItem( _
        ByVal index As Integer,
 ByVal newItem As String)

        MyBase.InsertItem(index, newItem)

        RaiseEvent Changed(Me, New
 DinosaursChangedEventArgs( _
            ChangeType.Added, newItem, Nothing))
    End Sub

    Protected Overrides Sub
 SetItem(ByVal index As Integer,
 _
        ByVal newItem As String)

        Dim replaced As String
 = Items(index)
        MyBase.SetItem(index, newItem)

        RaiseEvent Changed(Me, New
 DinosaursChangedEventArgs( _
            ChangeType.Replaced, replaced, newItem))
    End Sub

    Protected Overrides Sub
 RemoveItem(ByVal index As Integer)

        Dim removedItem As String
 = Items(index)
        MyBase.RemoveItem(index)

        RaiseEvent Changed(Me, New
 DinosaursChangedEventArgs( _
            ChangeType.Removed, removedItem, Nothing))
    End Sub

    Protected Overrides Sub
 ClearItems()
        MyBase.ClearItems()

        RaiseEvent Changed(Me, New
 DinosaursChangedEventArgs( _
            ChangeType.Cleared, Nothing, Nothing))
    End Sub

End Class

' Event argument for the Changed event.
'
Public Class DinosaursChangedEventArgs
    Inherits EventArgs

    Public ReadOnly ChangedItem As
 String
    Public ReadOnly ChangeType As
 ChangeType
    Public ReadOnly ReplacedWith As
 String

    Public Sub New(ByVal
 change As ChangeType, ByVal item As
 String, _
        ByVal replacement As String)

        ChangeType = change
        ChangedItem = item
        ReplacedWith = replacement
    End Sub
End Class

Public Enum ChangeType
    Added
    Removed
    Replaced
    Cleared
End Enum

Public Class Demo
    
    Public Shared Sub Main()
 

        Dim dinosaurs As New
 Dinosaurs

        AddHandler dinosaurs.Changed, AddressOf
 ChangedHandler

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

        Display(dinosaurs)
    
        Console.WriteLine(vbLf & "IndexOf(""Muttaburrasaurus""):
 {0}", _
            dinosaurs.IndexOf("Muttaburrasaurus"))

        Console.WriteLine(vbLf & "Contains(""Caudipteryx""):
 {0}", _
            dinosaurs.Contains("Caudipteryx"))

        Console.WriteLine(vbLf & "Insert(2, ""Nanotyrannus"")")
        dinosaurs.Insert(2, "Nanotyrannus")

        Console.WriteLine(vbLf & "dinosaurs(2): {0}",
 dinosaurs(2))

        Console.WriteLine(vbLf & "dinosaurs(2) = ""Microraptor""")
        dinosaurs(2) = "Microraptor"

        Console.WriteLine(vbLf & "Remove(""Microraptor"")")
        dinosaurs.Remove("Microraptor")

        Console.WriteLine(vbLf & "RemoveAt(0)")
        dinosaurs.RemoveAt(0)

        Display(dinosaurs)
 
    End Sub
    
    Private Shared Sub Display(ByVal
 cs As Collection(Of String))
 
        Console.WriteLine()
        For Each item As
 String In cs
            Console.WriteLine(item)
        Next item
    End Sub

    Private Shared Sub ChangedHandler(ByVal
 source As Object, _
        ByVal e As DinosaursChangedEventArgs)

        If e.ChangeType = ChangeType.Replaced Then
            Console.WriteLine("{0} was replaced with {1}",
 _
                e.ChangedItem, e.ReplacedWith)

        ElseIf e.ChangeType = ChangeType.Cleared Then
            Console.WriteLine("The dinosaur list was cleared.")

        Else
            Console.WriteLine("{0} was {1}.", _
                e.ChangedItem, e.ChangeType)
        End If
    End Sub

End Class

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

/* 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
 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からCollection.RemoveItem メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からCollection.RemoveItem メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からCollection.RemoveItem メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

Collection.RemoveItem メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Collection.RemoveItem メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS