在C#中为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
热门推荐
10 下雪宝宝生日祝福语简短
11 中秋送健康祝福语简短
12 导师退休花束祝福语简短
13 宝宝新生入学祝福语简短
14 夸赞老师祝福语搞笑简短
15 祝寿时祝福语简短精辟
16 怀念青春祝福语简短
17 发小结婚简短祝福语
18 幼教专干调动祝福语简短