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# | Add an object to the end of the ArrayList
Next article icon

C# | Adding elements to the end of the ArrayList

Last Updated : 01 Feb, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

AddRange(ICollection) Method is used to add the elements of an ICollection to the end of the ArrayList. Or in other words, this method is used to add the multiple elements from other collection into an ArrayList. Here elements are defined as the primitive or non-primitive type.

Syntax:

public virtual void AddRange (ICollection col);

Here, col is an ICollection whose elements should be added to the end of the ArrayList. The collection itself cannot be null, but it can contain null elements.

Exception:

  • If the value of col is null then this method will give ArgumentNullException.
  • This method will gives NotSupportedException if the ArrayList is read-only, or the ArrayList has a fixed size

Below given are some examples to understand the implementation in a better way:

Example 1:




// C# program to illustrate the AddRange() Method
using System;
using System.Collections;
  
class GFG {
  
    // Main method
    public static void Main()
    {
  
        // Creates and initializes a new ArrayList
        ArrayList mylist = new ArrayList();
        mylist.Add("C# ");
        mylist.Add("DSA ");
        mylist.Add("Java ");
        mylist.Add("CPP");
  
        // Creates and initializes a new Queue
        Queue mydata = new Queue();
        mydata.Enqueue("C ");
        mydata.Enqueue("Python ");
        mydata.Enqueue("HTML ");
        mydata.Enqueue("CSS ");
        mydata.Enqueue("JavaScript ");
        mydata.Enqueue("Ruby");
  
        // Displays the ArrayList and the Queue.
        Console.Write("The original ArrayList: ");
        Data(mylist);
          
        Console.Write("The original Queue: ");
        Data(mydata);
  
        // Copies the elements of the queue to the end of the ArrayList.
        mylist.AddRange(mydata);
  
        // Displays the new ArrayList.
        Console.Write("The new ArrayList is: ");
        Data(mylist);
    }
  
    // method to display the ArrayList
    // and Queue elements
    public static void Data(IEnumerable mylis)
    {
        foreach(string str in mylis)
            Console.Write("{0}", str);
        Console.WriteLine();
    }
}
 
 

Output:

  The original ArrayList: C# DSA Java CPP  The original Queue: C Python HTML CSS JavaScript Ruby  The new ArrayList is: C# DSA Java CPPC Python HTML CSS JavaScript Ruby  

Example 2:




// C# program to illustrate 
// the AddRange() Method
using System;
using System.Collections;
  
public class GFG {
  
    // Main method
    public static void Main()
    {
  
        // Creates and initializes a new ArrayList.
        ArrayList mylist = new ArrayList();
        mylist.Add(5);
        mylist.Add(10);
        mylist.Add(15);
        mylist.Add(20);
        mylist.Add(25);
  
        // Creates and initializes a new ArrayList.
        ArrayList mydata = new ArrayList();
        mydata.Add(30);
        mydata.Add(35);
        mydata.Add(40);
        mydata.Add(45);
        mydata.Add(50);
  
        // Displays both ArrayList.
        Console.WriteLine("The ArrayList 1: ");
        foreach(int i in mylist)
        {
            Console.WriteLine(i);
        }
        Console.WriteLine("The ArrayList 2: ");
        foreach(int j in mydata)
        {
            Console.WriteLine(j);
        }
  
        // Copies the elements of the mydata
        // to the end of the mylist
        mylist.AddRange(mydata);
  
        // Displays the new ArrayList.
        Console.WriteLine("The new ArrayList is :");
        for (int k = 0; k < mylist.Count; k++)
            Console.WriteLine(mylist[k]);
    }
}
 
 

Output:

  The ArrayList 1:   5  10  15  20  25  The ArrayList 2:   30  35  40  45  50  The new ArrayList is :  5  10  15  20  25  30  35  40  45  50  

Reference:

  • https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist.addrange?view=netframework-4.7.2#System_Collections_ArrayList_AddRange_System_Collections_ICollection_


Next Article
C# | Add an object to the end of the ArrayList
author
ankita_saini
Improve
Article Tags :
  • C#
  • CSharp-Collections-ArrayList
  • CSharp-Collections-Namespace
  • CSharp-method

Similar Reads

  • C# | Adding the elements to the end of the ArrayList
    ArrayList.AddRange(ICollection) Method is used to add the elements of an ICollection to the end of the ArrayList. Syntax: public virtual void AddRange (System.Collections.ICollection c); Here, c is the ICollection whose elements should be added to the end of the ArrayList. The collection itself cann
    2 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# | Copying the elements of ArrayList to a new array
    ArrayList.ToArray Method is used to copy the elements of the ArrayList to a new array. This method contains two methods in its overload list as follows: ToArray()ToArray(Type)ToArray() This method is used to copy the elements of the ArrayList to a new Object array. The elements are copied using Arra
    2 min read
  • C# | Adding an element to the List
    List.Add(T) Method is used to add an object to the end of the List. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot. List class can accept null as a valid value for reference types and it also allows duplicate elements. If the Count becomes eq
    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# | 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
  • C# | Insert an element into the ArrayList at the specified index
    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.Insert(Int32, Object) method inserts an element into the ArrayLis
    3 min read
  • C# | Sets the capacity to the actual number of elements in the ArrayList
    ArrayList.TrimToSize Method is used to set the capacity to the actual number of elements in the ArrayList. It can be used to minimize a collection's memory overhead if no new elements will be added to the collection. Note: This method is an O(n) operation, where n is Count. Syntax: public virtual vo
    3 min read
  • C# | Remove the element at the specified index 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.RemoveAt(Int32) method is used to remove the element at the speci
    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