

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the ArrayList class in C#?
<p>ArrayList class represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array.</p><p>The following are the methods of Arraylist class −</p><table class="table table-bordered"><thead><tr><th style="text-align:center;">Sr.No</th><th style="text-align:center;">Method & Description</th></tr></thead><tbody><tr><td class="ts">1</td><td><strong>public virtual int Add(object value);</strong><br>Adds an object to the end of the ArrayList.<br></td></tr><tr><td class="ts">2</td><td><strong>public virtual void AddRange(ICollection c);</strong><br>Adds the elements of an ICollection to the end of the ArrayList.<br></td></tr><tr><td class="ts">3</td><td><strong>public virtual void Clear();</strong><br>Removes all elements from the ArrayList.<br></td></tr><tr><td class="ts">4</td><td><strong>public virtual bool Contains(object item);</strong><br>Determines whether an element is in the ArrayList.<br></td></tr><tr><td class="ts">5</td><td><strong>public virtual ArrayList GetRange(int index, int count);</strong><br>Returns an ArrayList which represents a subset of the elements in the source ArrayList.<br></td></tr><tr><td class="ts">6</td><td><strong>public virtual int IndexOf(object);</strong><br>Returns the zero-based index of the first occurrence of a value in the ArrayList or in a portion of it.<br></td></tr><tr><td class="ts">7</td><td><strong>public virtual void Insert(int index, object value);</strong><br>Inserts an element into the ArrayList at the specified index.<br></td></tr><tr><td class="ts">8</td><td><strong>public virtual void InsertRange(int index, ICollection c);</strong><br>Inserts the elements of a collection into the ArrayList at the specified index.<br></td></tr><tr><td class="ts">9</td><td><strong>public virtual void Remove(object obj);</strong><br>Removes the first occurrence of a specific object from the ArrayList.<br></td></tr><tr><td class="ts">10</td><td><strong>public virtual void RemoveAt(int index);</strong><br>Removes the element at the specified index of the ArrayList.<br></td></tr><tr><td class="ts">11</td><td><strong>public virtual void RemoveRange(int index, int count);</strong><br>Removes a range of elements from the ArrayList.<br></td></tr><tr><td class="ts">12</td><td><strong>public virtual void Reverse();</strong><br>Reverses the order of the elements in the ArrayList.<br></td></tr><tr><td class="ts">13</td><td><strong>public virtual void SetRange(int index, ICollection c);</strong><br>Copies the elements of a collection over a range of elements in the ArrayList.<br></td></tr><tr><td class="ts">14</td><td><strong>public virtual void Sort();</strong><br>Sorts the elements in the ArrayList.<br></td></tr><tr><td class="ts">15</td><td><strong>public virtual void TrimToSize();</strong><br>Sets the capacity to the actual number of elements in the ArrayList.<br></td></tr></tbody></table><p>Let us see an example of ArrayList −</p><p>To sort an ArrayList in C#, use the Sort() method.</p><p>The following is the ArrayList −</p><pre class="result notranslate">ArrayList arr = new ArrayList(); arr.Add(32); arr.Add(12); arr.Add(55); arr.Add(8); arr.Add(13);</pre><p>Now the Sort() method is used to sort the ArrayList −</p><pre class="result notranslate">arr.Sort();</pre><p>You can try to run the following code to sort an ArrayList in C# −</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/1nyx2h" rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notranslate" style="">using System; using System.Collections; namespace Demo { class Program { static void Main(string[] args) { ArrayList arr = new ArrayList(); arr.Add(89); arr.Add(34); arr.Add(77); arr.Add(90); Console.Write("List: "); foreach (int i in arr) { Console.Write(i + " "); } Console.WriteLine(); Console.Write("Sorted List: "); arr.Sort(); foreach (int i in arr) { Console.Write(i + " "); } Console.WriteLine(); Console.ReadKey(); } } }</pre><h2>Output</h2><pre class="result notranslate">List: 89 34 77 90 Sorted List: 34 77 89 90</pre>
- Related Questions & Answers
- What is the Item property of ArrayList class in C#?
- What is the IsFixedSize property of ArrayList class in C#?
- What is the IsReadOnly property of ArrayList class in C#?
- What is the Count property of ArrayList class in C#?
- What is the Capacity property of an ArrayList class in C#?
- What is the class "class" in Java?
- What is the difference between Vector and ArrayList in Java?
- What is the difference between ArrayList and LinkedList in Java?
- How to use ArrayList class in C#?
- What is the super class of every class in Java?
- What is the Queue class in C#?
- What is the Hashtable class in C#?
- What is the BitArray class in C#?
- What is the Mutex class in C#?
- What is the SortedList class in C#?
Advertisements