- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Creating a read-only wrapper for the ArrayList in C#
To create a read-only wrapper for ArrayList, the code is as follows −
Example
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); } }
Output
This will produce the following output −
ArrayList elements... One Two Three Four Five Six Seven Eight ArrayList is read-only? = False
Example
Let us now see another example −
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); } }
Output
This will produce the following output −
ArrayList elements... One Two Three Four Five Six Seven Eight ArrayList is read-only? = False ArrayList is read-only now? = True
- Related Articles
- Creating a synchronized wrapper for the ArrayList in C#
- Creating a synchronized wrapper for the Hashtable in C#
- Creating a synchronized wrapper for a SortedList object in C#
- Check if the ArrayList is read-only in C#
- How to make Java ArrayList read only?
- How to make an ArrayList read only in Java?
- Creating an ArrayList having specified initial capacity in C#
- Get a read-only copy of the OrderedDictionary in C#
- Check if a SortedList is read-only in C#
- Check if the StringCollection is read-only in C#
- Check if the BitArray is read-only in C#
- How to add read-only property in C#?
- Check if Hashtable is read-only in C#
- Check if ListDictionary is read-only in C#
- C# Check if HybridDictionary is read only

Advertisements