

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Python program to list the difference between two lists.
- Program to find minimum difference between two elements from two lists in Python
- C# Program to get the difference between two dates
- C# Program to return the difference between two sequences
- How to compare two lists and add the difference to a third list in C#?
- C Program to calculate the difference between two time periods
- C# Program to determine the difference in hours between two dates
- C# Program to get the difference between two dates in seconds
- C++ Program to Calculate Difference Between Two Time Period
- C# program to concat two or more Lists
- C# program to find Intersection of two lists
- Program to interleave list elements from two linked lists in Python
- Java Program to Calculate the difference between two sets
- Program to find smallest difference between picked elements from different lists in C++
- Java Program to Join Two Lists
Advertisements