C#2.0中的HashSet替换 [英] HashSet replacement in C# 2.0

查看:86
本文介绍了C#2.0中的HashSet替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用 List< T> ,此列表包含数百个条目。我大量使用List.Contains方法,这会影响性能,我用字典替换了List,但是这导致了内存瓶颈,从而使性能变得更糟。有没有可以建议在List中搜索的更好的解决方案?在C#2.0中是否可以替换 HashSet< T> 或其他更好地兼顾内存和速度的方式?

解决方案

可以在位置使用 Dictionary< T,bool> HashSet< T> 的值。



它比 HashSet麻烦得多。



T> ,虽然不是很轻巧,但肯定比 List< T> 好。


I using List<T> in my project, this list contains hundreds of entries. I am using List.Contains method quite a lot and this is hurting performance, I replaced the List with dictionary but it resulted in memory bottleneck, thus made performance even worst. Is there a better solution that one can suggest for searching in List? Is there a replacement of HashSet<T> in C# 2.0 or some other way that is better both memory and speed wise?

解决方案

A Dictionary<T,bool> can be used in place of a HashSet<T>. Whether you add items with a value of True or False is a coin toss, the value is not relevant.

It's more cumbersome than a HashSet<T>, and not quite a light-weight, but it's certainly better than a List<T>.

这篇关于C#2.0中的HashSet替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆