TheDeveloperBlog.com

Home | Contact Us

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

C# Union Extension Method

This C# example set uses the Union extension method from System.Linq.

Union computes mathematical unions.

This extension method, from System.Linq, acts upon two collections. It returns a new collection that contains the elements that are found. It removes duplicates.

Extension Method

Example. First, Union() is accessed through the System.Linq namespace. So you will want to include the appropriate using-directive. The Union method will work with two collections of the same type of elements.

This includes List types and array types. In this example, we use integer arrays, but they could be string arrays or integer List types. The Union of 1, 2, 3 and 2, 3, 4 is 1, 2, 3, 4.

ListArrays

Also: Elements are here compared for equality by Union using the default comparison logic.

Based on:

.NET 4.5

C# program that invokes Union method

using System;
using System.Linq;

class Program
{
    static void Main()
    {
	// Create two example arrays.
	int[] array1 = { 1, 2, 3 };
	int[] array2 = { 2, 3, 4 };
	// Union the two arrays.
	var result = array1.Union(array2);
	// Enumerate the union.
	foreach (int value in result)
	{
	    Console.WriteLine(value);
	}
    }
}

Output

1
2
3
4

Example 2. Union does not sort. The first example makes the result appear sorted, but this is due simply to the already-sorted arrays. Here, I union two unsorted char arrays. The result is not sorted.

Char ArrayVarJoin

C# program that uses Union, unsorted chars

using System;
using System.Linq;

class Program
{
    static void Main()
    {
	char[] values1 = { 'a', 'z', 'c' };
	char[] values2 = { 'c', 's' };
	// Take union.
	var result = values1.Union(values2);
	Console.WriteLine(string.Join(",", result));
    }
}

Output

a,z,c,s

Summary. With Union, we combine collections. An imperative approach would involve a hash table or Dictionary. But the Union method can resolve duplicates automatically. This leads to higher levels of code simplicity.

Also: Please note the Intersect method. This returns only the shared elements in both collections it is called upon.

Intersect


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