Methods for Combining Lists in C# Programming
AddRange() Method
Syntax:
Foreach Loop Method
Syntax:
Enumerable.Concat() Method
Syntax:
Example # 1: By Using the AddRange() Method
We will demonstrate an example in which we use the AddRange() method for combining two different lists in C# programming. We perform the given examples in Ubuntu 20.04 text editor. When using the Ubuntu 20.04 text editor, we must save our file with the “.cs” extension. Then, explain the following code in detail:
We start our program with the “using System” library. We use this library to access classes and functions. It provides us with many valuable functions and classes. The “System.Collections.Generic ” is here for good performance containing different interfaces and classes which define generic collections. After invoking the “main” function, we create a new list named “Vegetables”, which is a string data type. We use the “new” keyword for creating a new list. When this new list is created, we have to pass some string data to this list. We add “Potato”, “Chilli”, “Garlic”, and “Tomato” to this list.
Now, we have to create another list so that we can combine both lists. So, we will form another list with the name “Fruits”. We created this list with the same method as we created the first one. When the second list is created, we add some fruits’ names to this list. Here, you can see that we add “Apple”, “Mango”, “Banana” and “Orange” to the second list. Now, it’s time to add both lists using the “AddRange” method. We use the same syntax of the AddRange() method previously discussed. Here, we give the name of the first list, which is “Vegetables”. Then, use the AddRange method and the name of the second list, “Fruits”.
In this code, the line “Vegetables.AddRange(Fruits)” is used for combining these two lists. After this, we will print this combined list with the help of “Console.WriteLine” statements. First, this statement prints the line. Then, we use String. Join (“,”, Vegetables) inside the Console.WriteLine statement to print the combined list. In this, “Join()” is a method to combine list elements using “,” to separate each element of the list.
As we use the Ubuntu 20.04 to perform these examples, we have to run some commands for the output on the terminal of Ubuntu 20.04. First, you must launch the Ubuntu terminal and write the “mcs” command. It is used for the compiling of our source code. When using this “mcs” command, we put the “.cs” extension with the file_name. You have to press Enter to run this “mcs” command. When this command runs successfully and there is no error in our code, it creates an executable file for us.
After that, we use another command which is the “mono” command here. This command is used for the execution of our code. When we use this command, we use the “.exe” file extension. Press Enter to run this “mono” command. The output of the code is rendered on the following screen:
In this output, you can see that it prints the two lists in a signal list, which means it combines both lists in one list and displays both lists in a single line or a single list.
Example # 2: By Using ForEach Loop Method
Now, we explore another example in which we use the ForEach Loop method to combine two lists. We perform different examples by using different methods for combining two lists.
The “Using System” is here, which contains classes and functions. Then, we have “System.Collections.Generic”, which contains interfaces and classes. The class is public with the name “Program”. We must have the “Main” function in our program. Then, we have to create two different and separate lists. We create the first list with the name “Stationery” by using the “new” keyword. We must add some elements to this list. So, we add “Pencil”, “Marker”, “Eraser”, and “Color” to this “Stationery” list.
After creating the first list, we create another list of “Subjects” and add different subject names to this second list. Here, we add “English”, “Computer”, “Biology”, and “Mathematics” to this “Subject” list. Now, we have to merge these two lists using the “ForEach” Loop method. When we use this ForEach loop method, we must follow the syntax of this method. First, we have to give the name of the second list and use the ForEach loop. Inside this ForEach loop, we give the name of any list with a fat arrow “=>”.
After this fat arrow gives the name of the first list and then puts “Add” and inside add again gives the name of any list. In this code “Subject.ForEach(Things => Stationery. Add (Things)” line is used to merge two lists using the ForEach loop method. Now, we print this merged list by using “Console.WriteLine”. We print this list with the same method we deliberated in our preceding example. The output of our code is provided in the image provided below:
Example # 3: By Using Enumerable.Concat() Method
We are performing this given example for you to easily learn how to use different methods to combine two lists.
Here, we create a list of the variable named “listA” by using the “new” keyword and a list of string data types. We add different elements using the “list_name.Add()” method. The name of the first list is “listA” here. We add “Computer”, “Keyboard”, and “Mouse” to this list by using the “Add ()” method. We will print the list first in this code and combine both lists after printing both lists separately. We print the first list using the foreach loop. We initialize a variable with the name “e”, which stores all the elements of the “listA” and prints all elements using the “Console.WriteLine” method.
After printing this first list, we create and print a new list name, “listB” with the same method that we used to create and print the first list, “listA”. When both lists are created, we combine these lists. And for combining, we use the third method, which is “Enumerable.Concat()”. We create another list named “FinalList” and initialize this with the “Enumerable.Concat()” method.
First, we give the name of the first list, “listA” then “Concat” inside this, we give the name of the second list, which is “listB” and the ToList(). The ToList() gets the elements from the list and returns the new list. After all this, we print the concatenate or combined list using the same method we used in our previous examples. The output of this code is in the following image:
Conclusion:
This tutorial discussed how to combine two lists in C# programming in Ubuntu 20.04 by using different methods. This tutorial explained three different methods for combining two lists. We have also provided screenshots of the codes along with the output of each code for your better understanding. In addition, we utilized three different examples in which we use three different methods for merging or combining two lists in C# programming. I hope you will easily learn this concept, and this tutorial will be helpful for you in the future.