
- 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 find Intersection of two lists
To find intersection of two lists in C#, use the Intersect() method.
The following is our list 1.
List<int> list1 = new List<int>(); list1.Add(2); list1.Add(3); list1.Add(5); list1.Add(7);
The following is our list 2.
List<int> list2 = new List<int>(); list2.Add(5); list2.Add(4); list2.Add(6); list2.Add(8);
The following is the code to find the intersection of two lists in C#.
Example
using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Demo { public class Program { public static void Main(String[] args) { List<int> list1 = new List<int>(); list1.Add(2); list1.Add(3); list1.Add(5); list1.Add(7); Console.WriteLine(list1.Count); List<int> list2 = new List<int>(); list2.Add(5); list2.Add(4); list2.Add(6); list2.Add(8); Console.WriteLine(list2.Count); List<int> common = list1.Intersect(list2).ToList(); Console.WriteLine(common.Count); } } }
Output
4 4 1
- Related Articles
- Python program to find Intersection of two lists?
- Intersection of Two Linked Lists in C++
- JavaScript Program for Finding Intersection of Two Sorted Linked Lists
- JavaScript Program for Finding Intersection Point of Two Linked Lists
- Find the Intersection Point of Two Linked Lists in Java
- Intersection of Two Linked Lists in Python
- C++ program to find union and intersection of two unsorted arrays
- C# program to find Union of two or more Lists
- Program to find median of two sorted lists in C++
- How to find the intersection between two or more lists in R?
- C# program to find additional values in two lists
- Python - Intersection of multiple lists
- Python program to find Cartesian product of two lists
- C# program to find common values from two or more Lists
- Golang program to find intersection of two sorted arrays using two pointer approach

Advertisements