从C#中的队列中删除所有对象
要从队列中删除所有对象,代码如下-
示例
using System; using System.Collections.Generic; public class Demo { public static void Main(){ Queue<string> queue = new Queue<string>(); queue.Enqueue("Gary"); queue.Enqueue("Jack"); queue.Enqueue("Ryan"); queue.Enqueue("Kevin"); queue.Enqueue("Mark"); queue.Enqueue("Jack"); queue.Enqueue("Ryan"); queue.Enqueue("Kevin"); Console.Write("Count of elements = "); Console.WriteLine(queue.Count); queue.Clear(); Console.Write("Count of elements (updated) = "); Console.WriteLine(queue.Count); } }
输出结果
这将产生以下输出-
Count of elements = 8 Count of elements (updated) = 0
示例
让我们看另一个例子-
using System; using System.Collections.Generic; public class Demo { public static void Main(){ Queue<string> queue = new Queue<string>(); queue.Enqueue("A"); queue.Enqueue("B"); queue.Enqueue("C"); queue.Enqueue("D"); queue.Enqueue("E"); Console.WriteLine("Count of elements = "+queue.Count); queue.Clear(); Console.Write("Count of elements (updated) = "+queue.Count); } }
输出结果
这将产生以下输出-
Count of elements = 5 Count of elements (updated) = 0