![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 print all the common elements of two lists
Firstly create the two lists −
List list1 = new List() {40, 20, 60, 3, 55}; List list2 = new List() {20, 70, 55, 80};
To find the common elements, use the Intersect −
list1.Intersect(list2)
The following is the complete code to find the common elements between the two lists −
Example
using System; using System.Linq; using System.Collections.Generic; namespace Demo { class Program { static void Main(string[] args) { List list1 = new List() {40, 20, 60, 3, 55}; List list2 = new List() {20, 70, 55, 80}; Console.WriteLine("Common elements:"); foreach(int value in list1.Intersect(list2)) Console.WriteLine(value); } } }
Output
Common elements: 20 55
Advertisements