C# - ArrayList.Add() Method with Example

C# ArrayList.Add() Method: Here, we are going to learn how to add an object to the end of the ArrayList in C#.Net? By Nidhi Last updated : April 03, 2023

ArrayList.Add() Method

The ArrayList.Add() method 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

C# program to add an object to the end of the ArrayList

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 »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.