What is the default equality comparer for a set type?

后端 未结 2 601
情歌与酒
情歌与酒 2020-12-09 03:03

In the MSDN API for the HashSet constructor with no arguments it states

Initializes a new instance of the HashSet class that is empty and uses the d

2条回答
  •  青春惊慌失措
    2020-12-09 03:40

    By default, it will delegate to EqualityComparer.Default. This returns a comparer that can compare two objects of type T.

    For a custom class, this does a few things in this order:

    • if the class implements IEquatable, it will delegate to the class's implementation of this interface
    • if the class has an Equals method defined, it will use that
    • as a last resort, it will use reference equality

提交回复
热议问题