What is the ArrayList class in C#?

CsharpProgrammingServer Side Programming

<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 &minus;</p><table class="table table-bordered"><thead><tr><th style="text-align:center;">Sr.No</th><th style="text-align:center;">Method &amp; 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 &minus;</p><p>To sort an ArrayList in C#, use the Sort() method.</p><p>The following is the ArrayList &minus;</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 &minus;</p><pre class="result notranslate">arr.Sort();</pre><p>You can try to run the following code to sort an ArrayList in C# &minus;</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/1nyx2h" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate" style="">using System; using System.Collections; namespace Demo { &nbsp; &nbsp;class Program { &nbsp; &nbsp; &nbsp; static void Main(string[] args) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ArrayList arr = new ArrayList(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr.Add(89); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr.Add(34); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr.Add(77); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr.Add(90); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.Write(&quot;List: &quot;); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;foreach (int i in arr) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Write(i + &quot; &quot;); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.Write(&quot;Sorted List: &quot;); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr.Sort(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;foreach (int i in arr) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Write(i + &quot; &quot;); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.ReadKey(); &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">List: 89 34 77 90 Sorted List: 34 77 89 90</pre>
raja
Updated on 20-Jun-2020 15:58:08

Advertisements