在C#中为ArrayList创建一个只读包装器
要为ArrayList创建一个只读包装器,代码如下-
示例
using System;
using System.Collections;
public class Demo {
public static void Main(){
ArrayList list = new ArrayList();
list.Add("One");
list.Add("Two");
list.Add("Three");
list.Add("Four");
list.Add("Five");
list.Add("Six");
list.Add("Seven");
list.Add("Eight");
Console.WriteLine("ArrayList elements...");
foreach(string str in list){
Console.WriteLine(str);
}
Console.WriteLine("ArrayList is read-only? = "+list.IsReadOnly);
}
}输出结果
这将产生以下输出-
ArrayList elements... One Two Three Four Five Six Seven Eight ArrayList is read-only? = False
示例
现在让我们来看另一个示例-
using System;
using System.Collections;
public class Demo {
public static void Main(){
ArrayList list = new ArrayList();
list.Add("One");
list.Add("Two");
list.Add("Three");
list.Add("Four");
list.Add("Five");
list.Add("Six");
list.Add("Seven");
list.Add("Eight");
Console.WriteLine("ArrayList elements...");
foreach(string str in list){
Console.WriteLine(str);
}
Console.WriteLine("ArrayList is read-only? = "+list.IsReadOnly);
ArrayList list2 = ArrayList.ReadOnly(list);
Console.WriteLine("ArrayList is read-only now? = "+list2.IsReadOnly);
}
}输出结果
这将产生以下输出-
ArrayList elements... One Two Three Four Five Six Seven Eight ArrayList is read-only? = False ArrayList is read-only now? = True
热门推荐
10 宝宝生病简短祝福语大全
11 新年给妈妈简短祝福语
12 宝宝新年祝福语大全简短
13 朋友祝福语两字简短
14 朋友明天手术祝福语简短
15 60岁长辈祝福语简短
16 送给客户的祝福语 简短
17 春节期间祝福语大全简短
18 朋友弟弟生日祝福语简短