InterviewSolution
Saved Bookmarks
| 1. |
How to Join two lists in C#? |
|
Answer» Two lists can be joined USING the AddRange() method. This method adds the elements of the specified list or collection at the END of the given list. The parameter of the AddRange() method is the list or collection whose elements are added at the end of the list. This list or collection can have elements that are null but it itself cannot be null. A program that DEMONSTRATES the join of two lists using the AddRange() method is given as follows: using System; using System.Collections.Generic; class Demo { public static void Main() { List<int> list1 = NEW List<int>(); list1.Add(7); list1.Add(1); list1.Add(5); list1.Add(9); list1.Add(3); List<int> list2 = new List<int>(); list2.Add(1); list2.Add(6); list2.Add(4); list1.AddRange(list2); Console.WriteLine("The elements in list 1 are:"); foreach (int i in list1) { Console.WriteLine(i); } } }The output of the above program is as follows: The elements in list 1 are: 7 1 5 9 3 1 6 4 |
|