- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to append a second list to an existing list in C#?
Use the AddRange() method to append a second list to an existing list.
Here is list one −
List < string > list1 = new List < string > (); list1.Add("One"); list1.Add("Two");
Here is list two −
List < string > list2 = new List < string > (); list2.Add("Three"); ist2.Add("Four");
Now let us append −
list1.AddRange(list2);
Let us see the complete code.
Example
using System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List < string > list1 = new List < string > (); list1.Add("One"); list1.Add("Two"); Console.WriteLine("First list..."); foreach(string value in list1) { Console.WriteLine(value); } Console.WriteLine("Second list..."); List < string > list2 = new List < string > (); list2.Add("Three"); list2.Add("Four"); foreach(string value in list2) { Console.WriteLine(value); } Console.WriteLine("After Append..."); list1.AddRange(list2); foreach(string value in list1) { Console.WriteLine(value); } } }
Advertisements