C# program to remove the first occurrence of a specific object from the ArrayList (ArrayList.Remove() Method)

C# ArrayList.Remove() Method: Here, we are going to learn how to remove the first occurrence of a specific object from the ArrayList in C#.Net?
Submitted by Nidhi, on May 24, 2021

The Remove() method for the ArrayList class is used to remove the first occurrence of the given object from the ArrayList.

Syntax:

    void ArrayList.Remove(object item);

Parameter(s):

  • item: An object to be deleted from the ArrayList.

Return value:

It does not return any value.

Exception(s):

  • System.NotSupportedException

Program:

The source code to remove the first occurrence of a specific object from the ArrayList is given below. The given program is compiled and executed successfully.

using System;
using System.Collections;

namespace mynamespace
{
    class Program
    {
        //Entry point of program
        static void Main(string[] args)
        {
            ArrayList List = new ArrayList();

            List.Add("ABC");
            List.Add("XYZ");
            List.Add("PQR");
            
            List.Add(1.15);
            List.Add(2.25);
            List.Add(3.35);
            List.Add(4.45);
            List.Add(5.55);

            object[] objArray1 = List.ToArray();

            Console.WriteLine("Values in array list before Remove");
            foreach (object val in objArray1)
            {
                Console.WriteLine(val);
            }
            
            List.Remove("XYZ");

            object[] objArray2 = List.ToArray();

            Console.WriteLine("\n\nValues in array list after remove");
            foreach (object val in objArray2)
            {
                Console.WriteLine(val);
            }
        }
    }
}

Output:

Values in array list before Remove
ABC
XYZ
PQR
1.15
2.25
3.35
4.45
5.55


Values in array list after remove
ABC
PQR
1.15
2.25
3.35
4.45
5.55
Press any key to continue . . .

C# ArrayList Class Programs »


ADVERTISEMENT
ADVERTISEMENT


What's New (MCQs)



Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.