Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add an object to the end of the ArrayList in C#
To add an object to the end of the ArrayList, the code is as follows −
Example
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 −
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
Advertisements