
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 Elements to the End of ArrayList in C#
To add elements 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("Andy"); list.Add("Gary"); list.Add("Katie"); list.Add("Amy"); Console.WriteLine("Elements in ArrayList..."); foreach (string res in list) { Console.WriteLine(res); } string[] strArr = { "John", "Jacob" }; list.AddRange(strArr); Console.WriteLine("Elements in ArrayList...UPDATED"); foreach(String str in list) { Console.WriteLine(str); } } }
Output
This will produce the following output −
Elements in ArrayList... Andy Gary Katie Amy Elements in ArrayList...UPDATED Andy Gary Katie Amy John Jacob
Example
Let us see another example −
using System; using System.Collections; public class Demo { public static void Main(String[] args) { ArrayList list = new ArrayList(); list.Add("Andy"); list.Add("Gary"); list.Add("Katie"); list.Add("Amy"); Console.WriteLine("Elements in ArrayList..."); foreach (string res in list) { Console.WriteLine(res); } string[] strArr = { "John", "Jacob" }; list.AddRange(strArr); Console.WriteLine("Elements in ArrayList...UPDATED"); foreach(String str in list) { Console.WriteLine(str); } string[] strArr2 = { "Tim", "Tom", "David" }; list.AddRange(strArr2); Console.WriteLine("Elements in ArrayList...UPDATED"); foreach(String str in list) { Console.WriteLine(str); } } }
Output
This will produce the following output −
Elements in ArrayList... Andy Gary Katie Amy Elements in ArrayList...UPDATED Andy Gary Katie Amy John Jacob Elements in ArrayList...UPDATED Andy Gary Katie Amy John Jacob Tim Tom David
Advertisements