用 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
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP