
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
C# program to list the difference between two lists
To get the difference between two lists, firstly set two lists in C# −
// first list List < string > list1 = new List < string > (); list1.Add("A"); list1.Add("B"); list1.Add("C"); list1.Add("D"); // second list List < string > list2 = new List < string > (); list2.Add("C"); list2.Add("D"); foreach(string value in list2) { Console.WriteLine(value); }
To get the difference, use IEnumerable and Except() as shown below. The difference is shown in the third list −
IEnumerable < string > list3; list3 = list1.Except(list2);
The following is the complete code −
Example
using System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List < string > list1 = new List < string > (); list1.Add("A"); list1.Add("B"); list1.Add("C"); list1.Add("D"); Console.WriteLine("First list..."); foreach(string value in list1) { Console.WriteLine(value); } Console.WriteLine("Second list..."); List < string > list2 = new List < string > (); list2.Add("C"); list2.Add("D"); foreach(string value in list2) { Console.WriteLine(value); } Console.WriteLine("Difference in the two lists..."); IEnumerable < string > list3; list3 = list1.Except(list2); foreach(string value in list3) { Console.WriteLine(value); } } }
Output
First list... A B C D Second list... C D Difference in the two lists... A B
- Related Articles
- Python program to list the difference between two lists.
- Python Program to Calculate the Symmetric Difference Between Two Lists
- Program to find minimum difference between two elements from two lists in Python
- How to compare two lists and add the difference to a third list in C#?
- C# Program to return the difference between two sequences
- C# Program to get the difference between two dates
- Program to interleave list elements from two linked lists in Python
- C# program to find Intersection of two lists
- C# program to concat two or more Lists
- Program to find smallest difference between picked elements from different lists in C++
- Divide the given linked list in two lists of size ratio p:q in C++ Program
- C Program to calculate the difference between two time periods
- Python program to create a sorted merged list of two unsorted lists
- C# program to find additional values in two lists
- C# program to print all the common elements of two lists

Advertisements