在 C# 中为 ArrayList 创建只读包装器
csharpserver side programmingprogramming更新于 2025/11/24 8:31:00
要为 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

