ArrayList表示可以单独索引的对象的有序集合。基本上,它是数组的替代方法。它还允许动态分配内存,添加,搜索和排序列表中的项目。 ArrayList.IsReadOnly属性用于检查ArrayList是否为只读。
特性:
- 可以随时在数组列表集合中添加或删除元素。
- 不能保证对ArrayList进行排序。
- ArrayList的容量是ArrayList可以容纳的元素数。
- 可以使用整数索引访问此集合中的元素。此集合中的索引从零开始。
- 它还允许重复的元素。
- 不支持将多维数组用作ArrayList集合中的元素。
句法:
public virtual bool IsReadOnly { get; }
返回值:如果ArrayList为只读,则此方法返回True ,否则返回False 。默认值为False 。
例子:
// C# code to check if the ArrayList
// is read-only or not
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Creating an ArrayList
ArrayList myList = new ArrayList();
// Adding elements to ArrayList
myList.Add("Geeks");
myList.Add("for");
myList.Add("Geeks");
myList.Add("Noida");
myList.Add("Geeks Classes");
myList.Add("Delhi");
// To check if the ArrayList is read-only or not
Console.WriteLine(myList.IsReadOnly);
}
}
输出:
False
笔记:
- 创建只读集合后,不允许添加,删除或修改元素。
- 检索此属性的值是O(1)操作。
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.arraylist.isreadonly?view=netframework-4.7.2