How to remove an element from Array List in C#?


Declare a new ArrayList and add elements to it.

ArrayList arr = new ArrayList();
arr.Add( "One" );
arr.Add( "Two" );
arr.Add( "Three" );
arr.Add( "Four" );

Now let’s say you need to remove the element “Three”. For that, use the Remove() method.

arr.Remove("Three");

The following is the complete example to remove an element from ArrayList −

Example

 Live Demo

using System;
using System.Collections;

class Demo {

   static void Main() {
      ArrayList arr = new ArrayList();
      arr.Add( "One" );
      arr.Add( "Two" );
      arr.Add( "Three" );
      arr.Add( "Four" );

      Console.WriteLine("ArrayList...");
      foreach(string str in arr) {
         Console.WriteLine(str);
      }

      arr.Remove("Three");
      Console.WriteLine("ArrayList after removing an element...");

      foreach(string str in arr) {
         Console.WriteLine(str);
      }

      Console.ReadLine();
   }
}

Output

ArrayList...
One
Two
Three
Four
ArrayList after removing an element...
One
Two
Four

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 22-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements