Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

Enumerable.Except: practical reference

Enumerable.Except: practical reference: The deferred result returns unique elements of the first source absent from the second under the comparer. The second source is buffered in a set.

Version: .NET LINQ; verified on .NET 10.0.14

Syntax

source.Except(/* overload-specific arguments */)

The deferred result returns unique elements of the first source absent from the second under the comparer. The second source is buffered in a set.

Use it for set difference when duplicate results must be removed.

This is not multiset subtraction: occurrence counts are not preserved. Memory cost depends on the second source and unique results.

An empty first source gives no result; an empty second acts like Distinct(first). A null source throws ArgumentNullException.

The complete program actually uses Enumerable.Except; its exact expected stdout is 1,3.

Examples

using System;
using System.Collections.Generic;
using System.Linq;

Console.WriteLine(string.Join(",", new[] { 1, 2, 2, 3 }.Except(new[] { 2 })));

Result

1,3

The complete program actually uses `Enumerable.Except`; its exact expected stdout is `1,3`.

Sources