C#のQueue.Countプロパティ
C#のQueue.Countプロパティは、キューに含まれる要素の数を取得するために使用されます。
構文
構文は次のとおりです-
public virtual int Count { get; } 例
例を見てみましょう-
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Queue<int> queue = new Queue<int>();
queue.Enqueue(100);
queue.Enqueue(200);
queue.Enqueue(300);
queue.Enqueue(400);
queue.Enqueue(500);
queue.Enqueue(600);
queue.Enqueue(700);
queue.Enqueue(800);
queue.Enqueue(900);
queue.Enqueue(1000);
Console.WriteLine("Queue...");
foreach(int i in queue) {
Console.WriteLine(i);
}
Console.WriteLine("Count of elements in the Queue = "+queue.Count);
queue.Clear();
Console.WriteLine("Count of elements in the Queue [Updated] = "+queue.Count);
}
} 出力
これにより、次の出力が生成されます-
Queue... 100 200 300 400 500 600 700 800 900 1000 Count of elements in the Queue = 10 Count of elements in the Queue [Updated] = 0
例
別の例を見てみましょう-
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Queue<int> queue = new Queue<int>();
queue.Enqueue(100);
queue.Enqueue(200);
queue.Enqueue(300);
Console.WriteLine("Queue...");
foreach(int i in queue) {
Console.WriteLine(i);
}
Console.WriteLine("Count of elements in the Queue = "+queue.Count);
queue.Enqueue(800);
queue.Enqueue(900);
queue.Enqueue(1000);
Console.WriteLine("Count of elements in the Queue [Updated] = "+queue.Count);
queue.Clear();
Console.WriteLine("Count of elements in the Queue [Updated] = "+queue.Count);
}
} 出力
これにより、次の出力が生成されます-
Queue... 100 200 300 Count of elements in the Queue = 3 Count of elements in the Queue [Updated] = 6 Count of elements in the Queue [Updated] = 0
-
C#のBitArrayクラスのCountプロパティとは何ですか?
Countプロパティを使用して、BitArrayクラスの要素の数をカウントします。 まず、BitArrayクラスを設定しましょう- BitArray arr = new BitArray(10); 次に、以下に示すようにCountプロパティを使用します- 例 using System; using System.Collections; public class Demo { public static void Main() { BitArray arr = new BitArray(10); &n
-
C#のArrayListクラスのCountプロパティとは何ですか?
ArrayListクラスのCountプロパティは、ArrayListの要素の数をカウントします。 まず、ArrayListに要素を追加します- ArrayList arrList = new ArrayList(); arrList.Add(98); arrList.Add(55); arrList.Add(65); arrList.Add(34); 次に、配列リストの数を取得します- arrList.Count 以下は、C#でCountプロパティを実装するためのコードです- 例 using System; using System.Collections; class Demo {