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

C#でSortedListオブジェクトのキーを取得する方法

C#のSortedListオブジェクトに格納されているキーを取得するには、Keysプロパティを使用します。このプロパティは、ICollectionインターフェースとしてキーのコレクションを返すため、foreachループなどで簡単に各キーへアクセスできます。

SortedList.Keysプロパティとは

Keysプロパティは、SortedList内のすべてのキーを、ソートされた順序で含むコレクションを返します。また、特定のキーのインデックス位置を調べたい場合は、IndexOfKey()メソッドが便利です。以下に具体的なサンプルコードを示します。

例1:基本的な使い方

using System;
using System.Collections;
public class Demo {
   public static void Main(String[] args) {
      SortedList list = new SortedList();
      list.Add("One", "Finance");
      list.Add("Two", "Marketing");
      list.Add("Three", "Sales");
      list.Add("Four", "Purchase");
      list.Add("Five", "Operations");
      list.Add("Six", "IT");
      Console.WriteLine("SortedList elements...");
      foreach(DictionaryEntry d in list) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("\nIndex at key One = "+list.IndexOfKey("One"));
      ICollection col = list.Keys;
      Console.WriteLine("\nCollection of Keys...");
      foreach(string res in col)
         Console.WriteLine(res);
   }
}

出力結果

上記のコードを実行すると、次のような出力が得られます。

SortedList elements...
Five Operations
Four Purchase
One Finance
Six IT
Three Sales
Two Marketing
Index at key One = 2
Collection of Keys...
Five
Four
One
Six
Three
Two

出力を見ると、SortedListはキーによって自動的にソートされており(アルファベット順)、IndexOfKey("One")はインデックス「2」を返していることがわかります。

例2:複数のSortedListからキーを取得する

次に、別の例として、数値型と文字列型の値を持つ2つのSortedListオブジェクトから、それぞれキーのコレクションを取得してみましょう。

using System;
using System.Collections;
public class Demo {
   public static void Main(String[] args) {
      SortedList list1 = new SortedList();
      list1.Add("One", 1);
      list1.Add("Two ", 2);
      list1.Add("Three ", 3);
      list1.Add("Four", 4);
      list1.Add("Five", 5);
      list1.Add("Six", 6);
      list1.Add("Seven ", 7);
      list1.Add("Eight ", 8);
      list1.Add("Nine", 9);
      list1.Add("Ten", 10);
      Console.WriteLine("SortedList1 elements...");
      foreach(DictionaryEntry d in list1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      ICollection col = list1.Keys;
      Console.WriteLine("\nCollection of Keys in List1...");
      foreach(string res in col)
         Console.WriteLine(res);
      SortedList list2 = new SortedList();
      list2.Add("A", "Accessories");
      list2.Add("B", "Books");
      list2.Add("C", "Smart Wearable Tech");
      list2.Add("D", "Home Appliances");
      Console.WriteLine("\nSortedList2 elements...");
      foreach(DictionaryEntry d in list2) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      col = list2.Keys;
      Console.WriteLine("\nCollection of Keys in List2...");
      foreach(string res in col)
         Console.WriteLine(res);
   }
}

出力結果

実行すると、以下の出力が表示されます。

SortedList1 elements...
Eight 8
Five 5
Four 4
Nine 9
One 1
Seven 7
Six 6
Ten 10
Three 3
Two 2

Collection of Keys in List1...
Eight
Five
Four
Nine
One
Seven
Six
Ten
Three
Two

SortedList2 elements...
A Accessories
B Books
C Smart Wearable Tech
D Home Appliances
Collection of Keys in List2...
A
B
C
D

ポイントまとめ

  • SortedList.Keysプロパティは、すべてのキーをICollectionとして取得できます。
  • キーは自動的にソートされた順序で返されます。
  • IndexOfKey()メソッドを使うと、指定したキーのインデックス位置を取得できます。
  • 値だけを取得したい場合は、同様にValuesプロパティを使用します。
  1. C#のSortedListクラスとは?特徴と基本的な使い方を解説

    SortedListクラスとは C#のSortedListクラスは、配列とハッシュテーブルを組み合わせたコレクションです。キーと値のペアからなるリストを保持しており、各要素には「キー」または「インデックス」のどちらからでもアクセスできます。 インデックスを使ってアクセスすればArrayListのように振る舞い、キーを使ってアクセスすればHashtableのように振る舞います。最大の特徴は、コレクション内の項目が常にキーの値に基づいて自動的にソートされるという点です。また、キーは重複できず、各キーには1つの値のみが対応します。 例:SortedListに要素を追加する Add()メソッドを使う

  2. 【C#】SortedListクラスのCountプロパティとは?使い方を実例で解説

    Countプロパティとは C#のSortedListクラスは、キーに基づいて自動的にソートされるキーと値のペアを管理するコレクションです。このクラスが持つCountプロパティを使うと、コレクション内に実際に格納されている要素(キーと値のペア)の数を取得できます。 Countプロパティは読み取り専用で、戻り値はint型です。なお、コレクションの内部容量を表すCapacityプロパティとは異なり、Countは「実際に追加されている要素数」を返すという点に注意してください。 Countプロパティの主な特徴 SortedListに格納されている要素の総数をint型で返す 読み取り専用のため、外部か