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
Adding the elements of the specified collection to the end of the List in C#
To add elements of the specified collection to the end of the List, the code is as follows −
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args){
List<string> list = new List<string>();
list.Add("Andy");
list.Add("Gary");
list.Add("Katie");
list.Add("Amy");
Console.WriteLine("Elements in List...");
foreach (string res in list){
Console.WriteLine(res);
}
string[] strArr = { "John", "Jacob" };
list.AddRange(strArr);
Console.WriteLine("Elements in List...UPDATED");
foreach(String str in list){
Console.WriteLine(str);
}
}
}
Output
This will produce the following output −
Elements in List... Andy Gary Katie Amy Elements in List...UPDATED Andy Gary Katie Amy John Jacob
Example
Let us now see another example −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args){
List<int> list = new List<int>();
list.Add(25);
list.Add(50);
list.Add(75);
list.Add(100);
Console.WriteLine("Elements in List...");
foreach (int val in list){
Console.WriteLine(val);
}
int[] intArr = { 150, 200, 250, 300 };
list.AddRange(intArr);
Console.WriteLine("Elements in List...UPDATED");
foreach(int val in list){
Console.WriteLine(val);
}
}
}
Output
This will produce the following output −
Elements in List... 25 50 75 100 Elements in List...UPDATED 25 50 75 100 150 200 250 300
Advertisements