Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# | Creating a read-only wrapper for the List
Next article icon

C# | Creating a read-only wrapper for the ArrayList

Last Updated : 01 Feb, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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:
  • https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist.readonly?view=netframework-4.7.2#System_Collections_ArrayList_ReadOnly_System_Collections_ArrayList_

Next Article
C# | Creating a read-only wrapper for the List

K

Kirti_Mangal
Improve
Article Tags :
  • C#
  • CSharp-method
  • CSharp-Collections-Namespace
  • CSharp-Collections-ArrayList

Similar Reads

  • C# | Creating a read-only wrapper for the List
    List<T>.AsReadOnly Method is used to get a read-only ReadOnlyCollection<T> 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<T>.AsReadOnly Method is used to get a read-only ReadOnlyCollection<T> wrapper for the current collection. Syntax: public System.Collections.ObjectModel.ReadOnlyCollection<T> 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences