C# | Creating a readonly wrapper for the ArrayList (original) (raw)
Last Updated : 01 Feb, 2019
ArrayList.ReadOnly(ArrayList) Method is used to get a read-only ArrayList wrapper.Syntax:
public static System.Collections.ArrayList ReadOnly(System.Collections.ArrayList list);
Here, the list is the ArrayList which is to be wrapped.Return Value: It returns a read-only ArrayList Wrapper around the list.Exception: This method returns the ArgumentNullException if the list is null. Below programs illustrate the use of above-discussed method:Example 1:
CSharp `
// C# code to create a read-only // wrapper for the ArrayList using System; using System.Collections;
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");
// Creating a Read-Only packing
// around the ArrayList
ArrayList myList2 = ArrayList.ReadOnly(myList);
// --------- Using IsReadOnly Property
// print the status of both ArrayList
Console.WriteLine("myList ArrayList is {0}.",
myList.IsReadOnly ? "read-only" :
"not read-only");
Console.WriteLine("myList2 ArrayList is {0}.",
myList2.IsReadOnly ? "read-only" :
"not read-only");
}
}
`
Output:
myList ArrayList is not read-only. myList2 ArrayList is read-only.
Example 2:
CSharp `
// C# code to create a read-only // wrapper for the ArrayList using System; using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating an ArrayList
ArrayList myList = new ArrayList();
// Adding elements to ArrayList
myList.Add("C");
myList.Add("C++");
myList.Add("Java");
myList.Add("C#");
myList.Add("Python");
Console.WriteLine("Before Wrapping: ");
// Displaying the elements in the ArrayList
foreach(string str in myList)
{
Console.WriteLine(str);
}
// Creating a Read-Only packing
// around the ArrayList
ArrayList myList2 = ArrayList.ReadOnly(myList);
Console.WriteLine("After Wrapping: ");
// Displaying the elements
foreach(string str in myList2)
{
Console.WriteLine(str);
}
Console.WriteLine("Trying to add new element into myList2:");
// it will give error
myList2.Add("HTML");
}
}
`
Output:
Before Wrapping: C C++ Java C# Python After Wrapping: C C++ Java C# Python Trying to add new element into myList2:
Runtime Error:
Unhandled Exception: System.NotSupportedException: Collection is read-only. at System.Collections.ArrayList+ReadOnlyArrayList.Add
Explanation: In the above program, you can add or remove the elements from the myList i.e original ArrayList which will reflect into the read-only collection.Note:
- To prevent any modifications to list, expose list only through this wrapper.
- A collection that is read-only is simply a collection with a wrapper that prevents modifying the collection. If changes are made to the underlying collection, the read-only collection reflects those changes.
- This method is an O(1) operation.
Reference:
Similar Reads
- C# | Creating a read-only wrapper for the List List.AsReadOnly Method is used to get a read-only ReadOnlyCollection wrapper for the current collection. Syntax: public System.Collections.ObjectModel.ReadOnlyCollection AsReadOnly (); Return Value: It returns an object that acts as a read-only wrapper around the current List<T 2 min read
- C# | Creating a read-only wrapper for List List.AsReadOnly Method is used to get a read-only ReadOnlyCollection wrapper for the current collection. Syntax: public System.Collections.ObjectModel.ReadOnlyCollection AsReadOnly (); Return Value: It returns an object that acts as a read-only wrapper around the current L 2 min read
- C# | Creating a synchronized (thread-safe) wrapper for the ArrayList Synchronized(ArrayList) method is used to get an ArrayList wrapper that is synchronized (thread safe). Syntax: public static System.Collections.ArrayList Synchronized (System.Collections.ArrayList list); Here, the list is the ArrayList which is to be synchronized. Return Value: It returns an ArrayLi 2 min read
- C# | Check if the ArrayList is read-only ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.IsReadOnly property is used to check whether the ArrayList is rea 2 min read
- How to create the ArrayList in C# ArrayList() constructor is used to initialize a new instance of the ArrayList class which will be empty and will have the default initial capacity. ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dyn 2 min read
- C# | Check if the BitArray is read-only The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e, 1 and false indicates the bit is off i.e, 0. This class is contained in System.Collections namespace. BitArray.IsReadOnly property is used to get a value indicati 2 min read
- C# | Remove all elements from the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Clear method is used to remove all the elements from the ArrayLis 3 min read
- C# | Remove a range of elements from the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.RemoveRange(Int32, Int32) method is used to remove a range of ele 3 min read
- C# | Add an object to the end of the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Add(Object) method adds an object to the end of the ArrayList. Pr 2 min read
- C# | Getting a subset of the elements from the source ArrayList ArrayList.GetRange(Int32, Int32) Method is used to get an ArrayList which will represent a subset of the elements in the source ArrayList. Syntax: public virtual System.Collections.ArrayList GetRange (int index, int count); Parameters: index: It is of Int32 type and represents the zero-based ArrayLi 3 min read