C# Enumerable Except(IEnumerable, IEnumerable)

Description

Produces the set difference of two sequences by using the default equality comparer to compare values.

Syntax


public static IEnumerable<TSource> Except<TSource>(
  this IEnumerable<TSource> first,
  IEnumerable<TSource> second
)

Parameters

  • TSource - The type of the elements of the input sequences.
  • first - An IEnumerable whose elements that are not also in second will be returned.
  • second - An IEnumerable whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence.

Example

The following code example demonstrates how to use the Except method to compare two sequences of numbers and return elements that appear only in the first sequence.


//w w w  .j  a  v a  2 s .  c o  m
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  

    double[] numbers1 = { 2.0, 2.1, 2.2, 2.3, 2.4, 2.5 };
    double[] numbers2 = { 2.2 };

    IEnumerable<double> onlyInFirstSet = numbers1.Except(numbers2);

    foreach (double number in onlyInFirstSet)
        Console.WriteLine(number);
  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Linq »




Enumerable