创建 ArrayList 的同步包装器
要为 ArrayList 创建同步包装器,代码如下 −
示例
using System; using System.Collections; public class Demo { public static void Main() { ArrayList arrList = new ArrayList(); arrList.Add("AB"); arrList.Add("CD"); arrList.Add("EF"); arrList.Add("GH"); arrList.Add("IJ"); arrList.Add("KL"); Console.WriteLine("ArrayList elements..."); foreach(string str in arrList) { Console.WriteLine(str); } Console.WriteLine("ArrayList is synchronized? = "+arrList.IsSynchronized); } }
输出
将生成以下输出 −
ArrayList elements... AB CD EF GH IJ KL ArrayList is synchronized? = False
示例
让我们看另一个示例 −
using System; using System.Collections; public class Demo { public static void Main() { ArrayList arrList = new ArrayList(); arrList.Add("AB"); arrList.Add("CD"); arrList.Add("EF"); arrList.Add("GH"); arrList.Add("IJ"); arrList.Add("KL"); Console.WriteLine("ArrayList elements..."); foreach(string str in arrList) { Console.WriteLine(str); } Console.WriteLine("ArrayList is synchronized? = "+arrList.IsSynchronized); ArrayList arrList2 = ArrayList.Synchronized(arrList); Console.WriteLine("ArrayList is synchronized? = "+arrList2.IsSynchronized); } }
输出
将生成以下输出 −
ArrayList elements... AB CD EF GH IJ KL ArrayList is synchronized? = False ArrayList is synchronized? = True
广告