HashSet.IntersectWith() in C#

admin Last updated on: April 13, 2023

HashSet.IntersectWith() in C#

April 2nd, 2009

I realize that real developers have been aware of this for years.  However I think it’s important to remember that I am not a real developer.  I am primarily an IT Pro who happens to be able to fake being a developer in the right circumstances.  I should also note that I’m writing this blog post almost exclusively so I’ll have a chance of remembering this in 3 months when I need to use it.

I ran across HashSets (introduced in .Net 3.5) recently and immediately realized where they would come in incredibly handy.  Especially as an IT Pro you often have two sets of lists of users and you need to find the users that exist in both lists.

//artificially create two lists of users
HashSetstring> group1 = new HashSetstring> 
     { "user1", "user2", "user3" };
HashSetstring> group2 = new HashSetstring> 
     { "user3", "user4", "user5" };
 
//modify group1 to contain only 
//items that also exist in group2
group1.IntersectWith(group2);
 
//print out users that existed in both lists
foreach (string user in group1)
{
     Console.WriteLine(user);
}

Related posts

Giới thiệu hình nền điện thoại Liên Quân đẹp miễn chê

Giới thiệu hình nền điện thoại Liên Quân đẹp miễn chê

Hình nền điện thoại Liên Quân đang trở thành xu hướng được giới trẻ vô cùng yêu thích. Đó...

Amazon Elastic Load Balancer Setup

Amazon Elastic Load Balancer Setup

Amazon Elastic Load Balancer Setup May 20th, 2009 As I previously wrote about, Amazon announced a load balancing solution...

Các nhân vật trong Ma Sói và chức năng đặc biệt của họ

Các nhân vật trong Ma Sói và chức năng đặc biệt của họ

Là board game phổ biến nhất thế giới, hệ thống các nhân vật trong Ma Sói vì vậy cũng...

Server Infrastructure Consulting We believe that IT can enhance your business model without getting in the way and without...

Cách lên đồ Sona tốc chiến cực kỳ hiệu quả cập nhật 2023

Cách lên đồ Sona tốc chiến cực kỳ hiệu quả cập nhật 2023

Sử dụng Sona tốc chiến hiệu quả chắc chắn sẽ giúp game thủ có thể leo Rank cực kỳ...

MSDN Events Unleashed

MSDN Events Unleashed

UPDATE: MSDN Events Unleashed: The Best of PDC coming to SLO March 9th, 2009 UPDATE – I just got...