C#のSortedListから指定したキーの要素を削除する方法(Removeメソッドの使い方)
C#のSortedListから、指定したキーを持つ要素を削除するにはRemove()メソッドを使用します。このメソッドは、引数に渡したキーと一致する要素(キーと値のペア)をコレクションから取り除きます。
Remove()メソッドの基本構文
public virtual void Remove(object key);
Remove()メソッドの主な特徴は以下の通りです。
- 指定したキーが存在する場合、その要素をSortedListから削除します。
- 指定したキーが存在しない場合でも例外はスローされず、SortedListは元のまま変化しません。
サンプルコード1
まずは、アルファベットのキーを持つSortedListから特定の要素を削除する例を見てみましょう。
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
SortedList sortedList = new SortedList();
sortedList.Add("A", "1");
sortedList.Add("B", "2");
sortedList.Add("C", "3");
sortedList.Add("D", "4");
sortedList.Add("E", "5");
sortedList.Add("F", "6");
sortedList.Add("G", "7");
sortedList.Add("H", "8");
sortedList.Add("I", "9");
sortedList.Add("J", "10");
Console.WriteLine("SortedList elements...");
foreach(DictionaryEntry d in sortedList) {
Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value);
}
Console.WriteLine("Count of SortedList key-value pairs = "+sortedList.Count);
sortedList.Remove("H");
Console.WriteLine("
Enumerator to iterate through the SortedList...");
IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
Console.WriteLine("Count of SortedList key-value pairs (Updated) = "+sortedList.Count);
}
}実行結果
上記のコードを実行すると、次のような出力が得られます。
SortedList elements... Key = A, Value = 1 Key = B, Value = 2 Key = C, Value = 3 Key = D, Value = 4 Key = E, Value = 5 Key = F, Value = 6 Key = G, Value = 7 Key = H, Value = 8 Key = I, Value = 9 Key = J, Value = 10 Count of SortedList key-value pairs = 10 Enumerator to iterate through the SortedList... Key = A, Value = 1 Key = B, Value = 2 Key = C, Value = 3 Key = D, Value = 4 Key = E, Value = 5 Key = F, Value = 6 Key = G, Value = 7 Key = I, Value = 9 Key = J, Value = 10 Count of SortedList key-value pairs (Updated) = 9
この例では、sortedList.Remove("H")を実行することで、キー「H」の要素だけが削除され、要素数が10から9に減っていることが確認できます。
サンプルコード2
続いて、文字列のキーと値を持つSortedListから要素を削除する別の例です。
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
SortedList sortedList = new SortedList();
sortedList.Add("One", "Mouse");
sortedList.Add("Two", "Keyboard");
sortedList.Add("Three", "Headphone");
sortedList.Add("Four", "Speakers");
sortedList.Add("Five", "RAM");
Console.WriteLine("SortedList elements...");
foreach(DictionaryEntry d in sortedList) {
Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value);
}
Console.WriteLine("Count of SortedList key-value pairs = "+sortedList.Count);
sortedList.Remove("Three");
Console.WriteLine("
Enumerator to iterate through the SortedList...");
IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
Console.WriteLine("Count of SortedList key-value pairs (Updated) = "+sortedList.Count);
}
}実行結果
上記のコードを実行すると、次のような出力が得られます。
SortedList elements... Key = Five, Value = RAM Key = Four, Value = Speakers Key = One, Value = Mouse Key = Three, Value = Headphone Key = Two, Value = Keyboard Count of SortedList key-value pairs = 5 Enumerator to iterate through the SortedList... Key = Five, Value = RAM Key = Four, Value = Speakers Key = One, Value = Mouse Key = Two, Value = Keyboard Count of SortedList key-value pairs (Updated) = 4
ここでは、sortedList.Remove("Three")によってキー「Three」の要素が削除され、要素数が5から4に更新されています。なお、SortedListはキーに基づいて自動的にソートされるため、出力結果は追加順ではなくキーの昇順で表示されている点にも注目してください。
まとめ
SortedListから特定のキーの要素を削除したい場合は、Remove()メソッドにキーを渡すだけで簡単に実現できます。存在しないキーを指定してもエラーにならないため、安全に呼び出せる一方で、削除が成功したかどうかを確認したい場合は、事前にContains()メソッドなどでキーの存在をチェックするとよいでしょう。
-
CSSでcontenteditable要素の境界線(枠線)を削除する方法
HTMLのcontenteditable属性を指定した要素は、ブラウザ上で直接テキストを編集できます。しかし、要素をクリックして編集モードにすると、多くのブラウザではデフォルトで青い枠線(アウトライン)が自動的に表示されます。この境界線を非表示にしたい場合は、CSSのoutlineプロパティを使用します。[contenteditable]セレクターに対してoutline: 0px solid transparent;を指定することで、編集時の枠線をきれいに取り除くことができます。コード例<!DOCTYPE html> <html> <head> <st
-
Windows Decrapifier&Debloaterを使用してWindows10から綿毛を取り除きます
リリースの時点で、Windows10はMicrosoftの最もスリムで平均的なOSでした。その前身であるWindows7よりもさらにジッパーのように感じられ、IoTデバイスで動作するのに十分コンパクトでした。しかし、それはその時でした。今日、Windows 10は、それを台無しにするのに十分な「機能」を蓄積しています。 多くのユーザーにとって、これらの「機能」は実際には役に立たないため、引用符を使用します。あなたも、最近Windows 10の速度が低下していることに気づき、Microsoftにその感想を伝えてもかまわない場合は(別名:テレメトリ機能を使用する)、OSをデクレイプする時期かもし