ArrayList Collection on in C# | A Complete tutorial For Beginners
ArrayList Collection on in C Tutorial ACTE

ArrayList Collection on in C# | A Complete tutorial For Beginners

Last updated on 27th Jan 2022, Blog, Tutorials

About author

Manish Kiran (C# Developer )

Manish Kiran, C# Developer Manish Kiran is a C# developer expert and subject specialist who has experience with Git, WPF, WinForms, C#,.Net, SQL, .NET Development, VB, .NET Framework,.NET Core, SVN, and Mercurial. His articles help the learners get insights into the domain.

(5.0) | 19005 Ratings 1471
    • Introduction to ArrayList Collection on in C#
    • ArrayList
    • What are Collections in C#?
    • Kinds of Collections
    • Constructors
    • ArrayList Properties
    • ArrayList iteration
    • Difference between Array and ArrayList
    • Points to recall
    • ArrayList Methods
    • The following figure shows the ArrayList class
    • Method description
    • Using the ArrayList class
    • Conclusion

    Subscribe For Free Demo

      Introduction to ArrayList Collection on in C#

      In C #, an ArrayList is a non-normal series of dynamically developing objects. It is similar to an array, besides that its length will increase dynamically. If you don`t recognise the kind and length of your data, you could use ArrayList to feature unknown data.


      ArrayList magnificence contained withinside the System.Collections namespace. Create an ArrayList item the usage of the key-word new.

      • Create an ArrayList
      • Use System.Collections.
      • ArrayList arlist = new ArrayList ();
      • // or
      • var arlist = new ArrayList (); // Recommended to feature factors to ArrayList
      • Add an detail to the ArrayList the usage of the Add () technique or the item initialization syntax. The ArrayList can include more than one null and copy values.
      • Example: Adding an detail to an ArrayList
      • // Add an detail the usage of the ArrayList.Add () technique
      • ar arlist1 = new ArrayList ();
      • arlist1.Add (1);
      • arlist1.Add (“invoice”);
      • arlist1.Add (“”);
      • arlist1.Add (true);
      • arlist1.Add (4.5);
      • arlist1.Add (null);
      • // Add an detail the usage of the item initialization syntax
      • var arlist2 = new ArrayList ()
      • ;

      Use the AddRange (ICollection c) method to add an Array, HashTable, SortedList, ArrayList, BitArray, Queue, and the entire Stack to an ArrayList.

      What are Collections in C#?

    • An assortment is a bunch of objects of comparative kind on which we might perform tasks like supplement, erase, update, sort, etc.
    • Collections, contrasted with clusters, are a more effective strategy for taking care of a bunch of related things or articles.
    • The objective of this article is to direct you on the best way to utilize Collections (type) in the C# programming language.
    • Collections are generally used to deal with and deal with a bunch of comparative information types. Clusters are likewise used to oversee comparative sorts of information, yet Collections offer extra adaptability when managing assembled things of various kinds.

    • Course Curriculum

      Learn Advanced C and C Plus Plus Certification Training Course to Build Your Skills

      Weekday / Weekend BatchesSee Batch Details

      Kinds of Collections

      C# Collections are sorted into 3 principle namespaces:

    • System.Collections.Generic classes (Generic)
    • System.Collections.Concurrent classes (Concurrent)
    • System.Collections classes (Non-Generic)
    • To add components to an assortment, we proclaim an occurrence of a class.

    • For instance, when we are utilizing components of similar information type, we proclaim System.Collections.Generic namespace which will import every one of the necessary classes.

      System.Collections.Generic classes (Generic). On the off chance that the components are of similar information type, we utilize one of the classes in the System.Collections.Generic namespace. The nonexclusive assortment just acknowledges one information type and no different information type.

      The classes given by the Generic Collections include:

    • List
    • Stack
    • Line
    • LinkedList
    • HashSet
    • SortedSet
    • Word reference
    • SortedDictionary
    • SortedList
    • System.Collections.Concurrent classes (Concurrent)

    • Simultaneous Collections are upheld in the .NET Framework 4 with the System.Collections.Concurrent namespace. They assist with admittance to assortment objects from a few strings. Assuming many strings need to get to an assortment simultaneously, the simultaneous Collections ought to be utilized rather than non-conventional and nonexclusive Collections.

      The classes given by the simultaneous Collections include:

    • BlockingCollection
    • ConcurrentBag
    • ConcurrentStack
    • ConcurrentQueue
    • ConcurrentDictionary
    • Partitioner
    • OrderablePartitioner
    • System.Collections classes (Non-Generic)

    • Dissimilar to the conventional assortment, non-nonexclusive Collections acknowledge components of various information types and use the System.Collections namespace. The classes given by the Non-Generic Collections include:

    • ArrayList
    • Stack
    • Line
    • Hashtable

    • Example:

      • Add the entire Array / ArrayList to the ArrayList
      • var arlist1 = new ArrayList ();
      • var arlist2 = new ArrayList ()
      • {
      • 1, “Bill”, “”, true, 4.5, null
      • };
      • int [] arr = {100, 200, 300, 400 };
      • queue myQ = new queue ();
      • myQ.Enqueue (“Hello”);
      • myQ.Enqueue (“World!”);
      • arlist1.AddRange (arlist2); // Add arraylist to arraylist
      • arlist1.AddRange (arr); // Add array to arraylist
      • arlist1.AddRange (myQ); // Add queue to ArrayList Access to ArrayList

      The ArrayList class implements the IList interface. Therefore, elements can be accessed using an indexer just like an array. The index starts at zero and increments by 1 for each subsequent element. Explicit cast to the appropriate type is required. Alternatively, use the var variable.


      ArrayList()- Initializes a new instance of the ArrayList class that is empty and has the default initial capacity.

      ArrayList(ICollection)- Initializes a new instance of the ArrayList class that contains elements copied from the specified collection and that has the same initial capacity as the number of elements copied.

      ArrayList(Int32)- Initializes a new instance of the ArrayList class that is empty and has the specified initial capacity.

      Example: Accessing elements of ArrayList

      • var arlist = new ArrayList ()
      • {
      • 1,
      • “Bill”,
      • 300,
      • 4.5f
      • };
      • // Access a single item with the indexer
      • int firstElement = (int) arlist [0]; // Returns 1
      • string secondElement = (string) arlist [1]; // Returns the “invoice”.
      • // int secondElement = (int) arlist [1]; // Error: Unable to cast string to int
      • // Use var keyword without explicit casting
      • var firstElement = arlist [0]; // returns 1
      • var secondElement = arlist [1]; // Returns the “invoice”.
      • // var fiveElement = arlist [5]; // Error: Index out of range
      • // Update item
      • arlist [0] = “Steve”;
      • artists [1] = 100;
      • // Arlist [5] = 500; // Error: Index out of range

      ArrayList Properties

      Course Curriculum

      Get JOB Oriented C and C Plus Plus Training for Beginners By MNC Experts

      • Instructor-led Sessions
      • Real-life Case Studies
      • Assignments
      Explore Curriculum

      ArrayList iteration

      ArrayList implements an ICollection interface that supports collection-type iterations. Therefore, iterate over the ArrayList using foreach and for loops. The Count property of the ArrayList returns the total number of elements in the ArrayList.


      • Iterate ArrayList
      • ArrayList arlist = new ArrayList ()
      • {
      • 1,
      • “Bill”,
      • 300,
      • 4.5F
      • };
      • foreach (arlist var item)
      • Console.Write (item + “,”); // Output: 1, Bill, 300, 4.5,
      • for (int i = 0; i
      • Console.Write (arlist [i] + “,”); // Output: 1, Bill, 300 , 4.5,
      • Insert elements into ArrayList
      • Use the Insert () method to insert an element into an ArrayList with the specified index.

      Difference between Array and ArrayList

      Points to recall

    • An ArrayList is an assortment of various sorts of things.
    • The distinction between an ArrayList and a cluster is that an ArrayList is dynamic in size and supports different component types.
    • An ArrayList can be introduced with introduction punctuation. That is to say, comma isolated qualities in a code block.
    • We can get to components in an ArrayList with the indexer, or with circles.
    • Since an ArrayList is dynamic in size, we utilize the Add() and AddRange() capacities to affix new things to the rundown.
    • The Add() capacity will enhance the finish of the ArrayList.
    • The AddRange() will add different things, as a cluster or assortment, to the furthest limit of the ArrayList.

    • ArrayList Methods

      The following figure shows the ArrayList class.

    • C # ArrayList
    • ArrayList property
    • Property description
    • Gets or sets the number of elements that the CapacityArrayList can hold.
    • Gets the number of elements actually contained in the CountArrayList.
    • Gets a value that indicates whether the size of the IsFixedSizeArrayList is fixed.
    • Gets a value that indicates whether IsReadOnlyArrayList is read-only.
    • Item Gets or sets an item at the specified index. ArrayList method

    • Method description

    • The Add () / AddRange () Add () methods add individual elements to the end of the ArrayList. The AddRange () method adds all the elements of the specified collection to the ArrayList. The Insert () / InsertRange () Insert () method inserts a single element into the ArrayList at the specified index.

    • The InsertRange () method inserts all the elements of the specified collection into the ArrayList, starting at the specified index. The Remove () / RemoveRange () Remove () method removes the specified element from the ArrayList. The RemoveRange () method removes the range of elements from the ArrayList. RemoveAt () Removes the element at the specified index from the ArrayList. Sort () Sorts the entire element of ArrayList. Reverse () Reverses the order of the elements in the entire ArrayList. Checks if the specified element it contains exists in the ArrayList. Returns true if it exists, false if it does not exist.

    • Clear Deletes all elements in the ArrayList. CopyTo Copies all elements or a range of elements into a compatible array. Returns the specified number of elements from the specified index of GetRangeArrayList. IndexOf Searches for the specified item and returns a zero-based index if found. Returns 1 if the item is not found. Returns a compatible array from ToArrayArrayList.

    • The following example shows how an ArrayList is created and initialized, and its values ​​are displayed.

      • C #
      • copy
      • With system;
      • Use System.Collections.
      • public class SamplesArrayList
      • {
      • public static void Main ()
      • {
      • // Create and initialize a new ArrayList.
      • ArrayList myAL = new ArrayList ();
      • myAL.Add (“Hello”);
      • myAL.Add (“World”);
      • myAL.Add (“!”);
      • // Show ArrayList properties and values increase.
      • Console.WriteLine (“myAL”);
      • Console.WriteLine (“
      • Count:
      • {0}”, myAL.Count);
      • Console.WriteLine (“
      • Capacity: {0}”, myAL.Capacity);
      • Console.Write (“
      • value:”);
      • PrintValues ​​(myAL);
      • }
      • public static void PrintValues ​​(IEnumerable myList)
      • {
      • foreach (Object obj in myList)
      • Console.Write (“
      • {0}”, obj);
      • Console.WriteLine ();
      • }
      • }
      • / *
      • This code produces output similar to the following:
      • myAL
      • Count:
      • 3
      • Capacity: 4
      • Value:
      • Hello
      • World
      • !
      • * /

      Using the ArrayList class

    • We do not recommend using the ArrayList class for new development. We recommend that you use the generic List class instead. The ArrayList class is designed to store a heterogeneous collection of objects. However, it is not always the best performance. Instead, we recommend the following: For a heterogeneous collection of objects, use List & lt; Object & gt ;. Type (for C #) or List (Of Object) (for Visual Basic). Use the List class for similar collections of objects. For a description of the relative performance of these classes, see “Performance Considerations” in the List Reference topic. For general information on how to use generic collections instead of non-generic collection types, see Don’t use non-generic collections on GitHub.

    • ArrayList is not guaranteed to be sorted. You must sort the ArrayList by calling the Sort method before performing any operation that requires sorting the ArrayList (such as BinarySearch). You can use the SortedSet class to maintain a collection that is automatically sorted when new items are added

    • ArrayList capacity is the number of elements that ArrayList can hold. When an element is added to the ArrayList, reallocation automatically increases its capacity as needed. You can reduce the capacity by calling TrimToSize or by explicitly setting the Capacity property.

    • .NET Framework only:

      For very large ArrayList objects, you can increase the maximum capacity to 2 billion elements on 64-bit systems by setting the enabled attribute of the component to true in the runtime environment.

    • Items in this collection can be accessed with an integer index. The index for this collection is zero-based.
    • The ArrayList collection accepts null as a valid value. It also allows duplicate elements.
    • Using multidimensional arrays as elements in an ArrayList collection is not supported.

    • C and C Plus Plus Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download


      A collection is a class, so you must declare an instance of the class before you can add elements to that collection. If your collection contains elements of only one data type, you can use one of the classes in the System. Collections. Generic namespace. Collection classes serve various purposes, such as allocating memory dynamically to elements and accessing a list of items on the basis of an index etc. These classes create collections of objects of the Object class, which is the base class for all data types in C#.

    Are you looking training with Right Jobs?

    Contact Us
    Get Training Quote for Free