用 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

Learn C# in-depth with real-world projects through our C# certification course. Enroll and become a certified expert to boost your career.

示例

现在我们再看另一个示例 −

 动态演示

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

更新于: 05-Dec-2019

138 次浏览

开启你的 职业生涯

完成课程获得认证

立即开始
广告