C# program to add an object to the end of the ArrayList (ArrayList.Add() Method)

C# ArrayList.Add() Method: Here, we are going to learn how to add an object to the end of the ArrayList in C#.Net?
Submitted by Nidhi, on May 24, 2021

The Add() method of the ArrayList class is used to add an item (an object) into the ArrayList. Items within the list can be integer, floating-point, or string value.

Syntax:

    int ArrayList.Add(object item);

Parameter(s):

  • item: An object to be added in the ArrayList.

Return value:

It returns the index where object has been added.

Exception(s):

  • System.NotSupportedException

Program:

The source code to add an object to the end of 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(10);
            List.Add(20);
            List.Add(30);
            List.Add(40);
            List.Add(50);

            List.Add("India");
            List.Add("Srilanka");
            List.Add("Nepal");
            List.Add("Bhutan");


            object[] objArray = List.ToArray();

            Console.WriteLine("Values in array list");
            foreach (object val in objArray)
            {
                Console.WriteLine(val);
            }
        }
    }
}

Output:

Values in array list
10
20
30
40
50
India
Srilanka
Nepal
Bhutan
Press any key to continue . . .

C# ArrayList Class Programs »


ADVERTISEMENT
ADVERTISEMENT




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.