C#中的Queue.Clone()方法用于创建Queue的浅表副本。
语法如下-
public virtual object Clone ();
现在让我们看一个例子-
using System; using System.Collections; public class Demo { public static void Main(string[] args) { Queue queue = new Queue(); queue.Enqueue("One"); queue.Enqueue("Two"); queue.Enqueue("Three"); queue.Enqueue("Four"); queue.Enqueue("Five"); queue.Enqueue("Six"); queue.Enqueue("Seven"); queue.Enqueue("Eight"); Console.WriteLine("Queue..."); foreach(string str in queue) { Console.WriteLine(str); } Queue queue2 = (Queue)queue.Clone(); Console.WriteLine("\nCloned Queue...."); foreach(string str in queue2) { Console.WriteLine(str); } } }
输出结果
Queue... One Two Three Four Five Six Seven Eight Cloned Queue.... One Two Three Four Five Six Seven Eight
现在让我们来看另一个例子-
using System; using System.Collections; public class Demo { public static void Main(string[] args) { Queue queue = new Queue(); queue.Enqueue(10); queue.Enqueue(20); queue.Enqueue(30); queue.Enqueue(40); queue.Enqueue(50); Console.WriteLine("Queue..."); foreach(int str in queue) { Console.WriteLine(str); } Queue queue2 = (Queue)queue.Clone(); Console.WriteLine("\nCloned Queue...."); foreach(int str in queue2) { Console.WriteLine(str); } } }
输出结果
这将产生以下输出-
Queue... 10 20 30 40 50 Cloned Queue.... 10 20 30 40 50