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

CsharpProgrammingServer Side Programming

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
raja
Published on 20-Aug-2018 16:14:24
Advertisements