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# Except (Remove Elements From Collection)

Use the Except method to subtract an entire collection from another.
Except subtracts elements from a collection. This extension method is found in the System.Linq namespace in the .NET Framework. It essentially subtracts all the elements in one collection from another.ExtensionLINQ
To start, this program uses two integer arrays. The second array contains 2 of the same elements as the first. Next the Except method is called upon the first array with the second array as the argument.Int Array

Result: Here Except() returns a collection where the second array's elements are subtracted from the first.

Not found: No errors occur when Except() is called and some of the elements in the second collection are not found in the first collection.

And: The elements are ignored in the computation. Except() is not useful for validating that one collection is contained within another.

C# program that calls Except method using System; using System.Linq; class Program { static void Main() { // Contains four values. int[] values1 = { 1, 2, 3, 4 }; // Contains three values (1 and 2 also found in values1). int[] values2 = { 1, 2, 5 }; // Remove all values2 from values1. var result = values1.Except(values2); // Show. foreach (var element in result) { Console.WriteLine(element); } } } Output 3 4
Discussion. Often, methods like Except are not as easy to understand as other approaches. When developing programs, the clearest approach is often best. We can use a looping construct to implement the functionality of Except.

And: It takes more code, and is not as impressive to read, but may be easier for other developers to understand it.

Summary. The Except extension method provides a fast way to use set logic. It removes all the elements from one array that are found in another array. This reduces the need for complex foreach-loops.ArrayForeach
© 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