How to remove an item from an ArrayList in C#?

CsharpProgrammingServer Side Programming

Firstly, set a a new ArrayList and add elements to it.

ArrayList arr = new ArrayList();
arr.Add( "Jones" );
arr.Add( "Tom" );
arr.Add( "Henry" );

Now let’s remove the item “Tom”. For that, use the Remove() method.

arr.Remove("Tom");

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( "Jones" );
      arr.Add( "Tom" );
      arr.Add( "Henry" );

      Console.WriteLine("Initial 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

Initial ArrayList...
Jones
Tom
Henry
ArrayList after removing an element...
Jones
Tom
Henry
raja
Published on 20-Aug-2018 12:47:08
Advertisements