C#のSortedListに含まれる要素の数を取得する方法
C#のSortedListコレクションに格納されている要素(キーと値のペア)の数を取得するには、Countプロパティを使用します。Countプロパティは、SortedList内に現在存在する要素の総数を整数値として返します。
また、SortedListからすべての要素を削除したい場合はClear()メソッドを使います。Clear()を実行すると、すべての要素が削除され、Countは0になります。
例:CountプロパティとClear()メソッドの使用
以下のコードでは、SortedListに10個のキーと値のペアを追加し、Countプロパティで要素数を確認した後、Clear()メソッドですべての要素を削除しています。
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.Clear();
Console.WriteLine("Count of SortedList (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 Count of SortedList (updated) = 0
このように、要素追加後のCountは「10」ですが、Clear()メソッドを呼び出した後は「0」になっていることがわかります。
例:GetEnumerator()で列挙子を使って反復処理する
続いて、IDictionaryEnumeratorを使用して、SortedListの各要素を順番に取り出す例を見てみましょう。
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args){
SortedList sortedList = new SortedList();
sortedList.Add("One", "1");
sortedList.Add("Two", "2");
sortedList.Add("Three", "3");
sortedList.Add("Four", "4");
sortedList.Add("Five", "5");
sortedList.Add("Six", "6");
sortedList.Add("Seven", "7");
sortedList.Add("Eight", "8");
sortedList.Add("Nine", "9");
sortedList.Add("Ten", "10");
Console.WriteLine("Count of SortedList key-value pairs = "+sortedList.Count);
Console.WriteLine("Enumerator to iterate through the SortedList...");
IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}出力結果
このコードを実行すると、次の出力が生成されます。
Count of SortedList key-value pairs = 10 Enumerator to iterate through the SortedList... Key = Eight, Value = 8 Key = Five, Value = 5 Key = Four, Value = 4 Key = Nine, Value = 9 Key = One, Value = 1 Key = Seven, Value = 7 Key = Six, Value = 6 Key = Ten, Value = 10 Key = Three, Value = 3 Key = Two, Value = 2
ポイントまとめ
- Countプロパティ:SortedListに含まれるキーと値のペアの数を取得できます。
- Clear()メソッド:SortedListからすべての要素を削除し、Countを0にします。
- GetEnumerator()メソッド:IDictionaryEnumeratorを返し、MoveNext()を使って各要素を順に反復処理できます。
- SortedListはキーによって自動的にソートされるため、出力結果もキーの昇順に並んでいます。
-
C#でファイルサイズ(バイト数)を取得する方法
C#でファイルのバイト数を確認したい場合は、FileInfo クラスが持つ Length プロパティを使うのが最も簡単です。このプロパティは、指定したファイルのサイズをバイト単位の long 型の値として返します。基本的な手順まず、対象となるファイルへのパスを指定して FileInfo オブジェクトを作成します。FileInfo file = new FileInfo(D:\\new);続いて、Length プロパティにアクセスすることで、ファイルのバイト数を取得できます。file.Lengthサンプルコード以下に、ファイルのバイト数を取得してコンソールに表示する完全なコードを示します。usi
-
C#でListの要素範囲を取得する方法:GetRange()メソッドの使い方
C#のListで特定の範囲の要素を取得するには、GetRange() メソッドを使用します。このメソッドは、指定したインデックス位置から指定した数の要素を取り出し、新しいリストとして返します。 GetRange() メソッドの基本構文 public List<T> GetRange(int index, int count); index:取得を開始する要素のインデックス(0から始まります) count:取得する要素の数 リストの作成と要素の追加 まず、リストを作成して要素を追加します。 List<int> arr1 = new List<int>();