TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Concat Extension: Combine Lists and Arrays

Use the Concat extension method from System.Linq. Concat combines 2 IEnumerables.
Concat. This is an extension method. With it we take 2 IEnumerable collections. We then get a collection with all of the elements together.
This extension method effectively concatenates sequences. We can combine Lists or arrays (and even different IEnumerables) together.IEnumerable
Example. In this program, we include the System.Linq namespace to get access to the Concat method. Two arrays are created upon execution of the Main method.LINQ

Then: We call the Concat method twice, in different orders, and display the results.

Tip: If you have ever needed to write custom code to combine two arrays or Lists into a single one, the Concat method might be useful.

List

Note: This might cause some performance degradation because of the flexibility and implementation of the extension methods.

Extension
C# program that uses Concat extension using System; using System.Linq; class Program { static void Main() { int[] array1 = { 1, 3, 5 }; int[] array2 = { 0, 2, 4 }; // Concat array1 and array2. var result1 = array1.Concat(array2); foreach (int value in result1) { Console.WriteLine(value); } Console.WriteLine(); // Concat array2 and then array1. var result2 = array2.Concat(array1); foreach (int value in result2) { Console.WriteLine(value); } } } Output 1 3 5 0 2 4 0 2 4 1 3 5
List example. To use Concat, you can combine two collections that implement IEnumerable. This includes the List type or the array type.

Info: You can combine a List and an array or two Lists. The element type (string) of both collections must be the same.

Return: Concat returns an IEnumerable type. The "var result" in the code is of that type.

Also: You can convert an IEnumerable back into a List with the ToList extension method.

ToList
C# program that uses Concat method on Lists using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { List<string> list1 = new List<string>(); list1.Add("dot"); list1.Add("net"); List<string> list2 = new List<string>(); list2.Add("Codex"); list2.Add("!"); var result = list1.Concat(list2); List<string> resultList = result.ToList(); foreach (var entry in resultList) { Console.WriteLine(entry); } } } Output dot net Codex !
A summary. If you have two Lists, you can use the Concat method to combine them. For optimal performance, however, using a method such as AddRange would be better if you need a List result.
Much like string.Concat concatenates strings, the Concat extension concatenates collections. Whenever we need to combine two arrays into a third array, we use the Concat extension method.string.Concat
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf