Add an object to the end of the ArrayList in C#

CsharpServer Side ProgrammingProgramming

To add an object to the end of the ArrayList, the code is as follows −

Example

 Live Demo

using System;
using System.Collections;
public class Demo {
   public static void Main(String[] args){
      ArrayList list = new ArrayList();
      list.Add("Tim");
      list.Add("Katie");
      list.Add("Amy");
      list.Add("Carlos");
      list.Add("Chris");
      list.Add("Jason");
      Console.WriteLine("Elements in ArrayList...");
      foreach (string res in list){
         Console.WriteLine(res);
      }
   }
}

Output

This will produce the following output −

Elements in ArrayList...
Tim
Katie
Amy
Carlos
Chris
Jason

Example

Let us now see another example −

 Live Demo

using System;
using System.Collections;
public class Demo {
   public static void Main(String[] args){
      ArrayList list1 = new ArrayList();
      list1.Add("A");
      list1.Add("B");
      list1.Add("C");
      list1.Add("D");
      list1.Add("E");
      list1.Add("F");
      list1.Add("G");
      list1.Add("H");
      list1.Add("I");
      Console.WriteLine("Elements in ArrayList1...");
      foreach (string res in list1){
         Console.WriteLine(res);
      }
      ArrayList list2 = new ArrayList();
      list2.Add("1");
      list2.Add("2");
      list2.Add("3");
      list2.Add("4");
      list2.Add("5");
      list2.Add("6");
      Console.WriteLine("Elements in ArrayList2...");
      foreach (string res in list2){
         Console.WriteLine(res);
      }
      Console.WriteLine("Is ArrayList1 equal to ArrayList2? = "+list1.Equals(list2));
   }
}

Output

This will produce the following output −

Elements in ArrayList1...
A
B
C
D
E
F
G
H
I
Elements in ArrayList2...
1
2
3
4
5
6
Is ArrayList1 equal to ArrayList2? = False
raja
Published on 06-Dec-2019 08:04:37
Advertisements