简体   繁体   English

c#类作为HashSet中的键

[英]c# Class as key in HashSet

I am using a HashSet where I use instances of classes as keys, that is, the references to them. 我正在使用HashSet ,其中将类的实例用作键,即对它们的引用。 What I am unsure of, is if the HashSet will compare the references, which would mean every instance would be different, or the actual values contained in the object, which is what I need. 我不确定的是, HashSet是否会比较引用,这意味着每个实例都会不同,或者对象中包含的实际值正​​是我所需要的。

Does a HashSet compare by value or reference, and if it is the latter, what would be a good way to let it compare by value? HashSet是否按值或引用进行比较,如果是后者, HashSet值比较的一种好方法是什么?

This link can help you : How does HashSet compare elements for equality? 该链接可以帮助您: HashSet如何比较元素是否相等?

You'll be able to redefine how comparison is done if it doesn't suit your needs. 如果比较不适合您,您将可以重新定义比较方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM