C# program to list the difference between two lists

To find the difference between two lists in C#, you can use the Except() method from LINQ. This method returns elements from the first list that are not present in the second list, effectively giving you the set difference.

Syntax

Following is the syntax for using Except() method to find list difference −

IEnumerable<T> result = list1.Except(list2);

Where list1 is the source list and list2 contains elements to exclude from the result.

Using Except() Method

The Except() method performs a set difference operation, returning elements that exist in the first collection but not in the second −

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

public class Demo {
   public static void Main() {
      List<string> list1 = new List<string> {"A", "B", "C", "D"};
      List<string> list2 = new List<string> {"C", "D"};

      Console.WriteLine("First list:");
      foreach(string value in list1) {
         Console.WriteLine(value);
      }

      Console.WriteLine("\nSecond list:");
      foreach(string value in list2) {
         Console.WriteLine(value);
      }

      Console.WriteLine("\nDifference (list1 - list2):");
      IEnumerable<string> difference = list1.Except(list2);
      foreach(string value in difference) {
         Console.WriteLine(value);
      }
   }
}

The output of the above code is −

First list:
A
B
C
D

Second list:
C
D

Difference (list1 - list2):
A
B

Bidirectional List Difference

To find elements that are different in both directions (symmetric difference), you can combine Except() operations −

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

public class Demo {
   public static void Main() {
      List<string> list1 = new List<string> {"A", "B", "C"};
      List<string> list2 = new List<string> {"C", "D", "E"};

      Console.WriteLine("First list: " + string.Join(", ", list1));
      Console.WriteLine("Second list: " + string.Join(", ", list2));

      Console.WriteLine("\nElements only in list1:");
      var onlyInList1 = list1.Except(list2);
      Console.WriteLine(string.Join(", ", onlyInList1));

      Console.WriteLine("\nElements only in list2:");
      var onlyInList2 = list2.Except(list1);
      Console.WriteLine(string.Join(", ", onlyInList2));

      Console.WriteLine("\nSymmetric difference:");
      var symmetricDiff = list1.Except(list2).Union(list2.Except(list1));
      Console.WriteLine(string.Join(", ", symmetricDiff));
   }
}

The output of the above code is −

First list: A, B, C
Second list: C, D, E

Elements only in list1:
A, B

Elements only in list2:
D, E

Symmetric difference:
A, B, D, E

Working with Custom Objects

When working with custom objects, Except() uses the default equality comparer. For custom comparison logic, you can implement IEqualityComparer

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

public class Person {
   public int Id { get; set; }
   public string Name { get; set; }
   
   public override bool Equals(object obj) {
      return obj is Person person && Id == person.Id;
   }
   
   public override int GetHashCode() {
      return Id.GetHashCode();
   }
   
   public override string ToString() {
      return $"{Id}: {Name}";
   }
}

public class Demo {
   public static void Main() {
      List<Person> team1 = new List<Person> {
         new Person { Id = 1, Name = "Alice" },
         new Person { Id = 2, Name = "Bob" },
         new Person { Id = 3, Name = "Charlie" }
      };

      List<Person> team2 = new List<Person> {
         new Person { Id = 2, Name = "Bob" },
         new Person { Id = 4, Name = "David" }
      };

      Console.WriteLine("Team1 members not in Team2:");
      var difference = team1.Except(team2);
      foreach(var person in difference) {
         Console.WriteLine(person);
      }
   }
}

The output of the above code is −

Team1 members not in Team2:
1: Alice
3: Charlie

Conclusion

The Except() method in C# provides an efficient way to find the difference between two lists by returning elements from the first list that are not present in the second. For bidirectional differences, combine multiple Except() operations or use Union() for symmetric differences.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements