C#
 Computer >> コンピューター >  >> プログラミング >> C#

指定されたキーを持つ要素をC#のSortedListから削除します


指定されたキーを持つ要素を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("\nEnumerator 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

別の例を見てみましょう-

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("\nEnumerator 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

  1. CSSを使用して編集可能な要素から境界線を削除するにはどうすればよいですか?

    編集可能な要素から境界線を削除するには、コードは次のとおりです- 例 <!DOCTYPE html> <html> <head> <style>    body{       font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;    }    p{       font-size: 40px;    }   &n

  2. Windows Decrapifier&Debloaterを使用してWindows10から綿毛を取り除きます

    リリースの時点で、Windows10はMicrosoftの最もスリムで平均的なOSでした。その前身であるWindows7よりもさらにジッパーのように感じられ、IoTデバイスで動作するのに十分コンパクトでした。しかし、それはその時でした。今日、Windows 10は、それを台無しにするのに十分な「機能」を蓄積しています。 多くのユーザーにとって、これらの「機能」は実際には役に立たないため、引用符を使用します。あなたも、最近Windows 10の速度が低下していることに気づき、Microsoftにその感想を伝えてもかまわない場合は(別名:テレメトリ機能を使用する)、OSをデクレイプする時期かもし