- 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 join two lists in C#?
To join two lists, use AddRange() method.
Set the first list −
var list1 = new List < string > (); list1.Add("Keyboard"); list1.Add("Mouse");
Set the second list −
var list2 = new List < string > (); list2.Add("Hard Disk"); list2.Add("Pen Drive");
To concatenate both the lists −
lists1.AddRange(lists2);
The following is the complete code −
Example
using System.Collections.Generic; using System; namespace Demo { public static class Program { public static void Main() { var list1 = new List < string > (); list1.Add("Keyboard"); list1.Add("Mouse"); Console.WriteLine("Our list1...."); foreach(var p in list1) { Console.WriteLine(p); } var list2 = new List < string > (); list2.Add("Hard Disk"); list2.Add("Pen Drive"); Console.WriteLine("Our list2...."); foreach(var p in list2) { Console.WriteLine(p); } list1.AddRange(list2); Console.WriteLine("Concatenated list...."); foreach(var p in list1) { Console.WriteLine(p); } } } }
Advertisements