如何在 .NET 中进行引用比较?
How to do reference comparison in .NET?
在Equality and Comparison Constraints in F#中表示:
Note there is no such thing as “reference comparison” (the object pointers used by .NET move around, so the ordering would change). You might implement that by using a unique tag and custom comparison.
如何在 F# 和 C# 中使用唯一标记进行比较?
How to do reference comparison in .NET? How can I use an unique tag for comparison in F# and C#?
请注意,在此上下文中,单词 "comparison" 与 "equality" 不同。它描述了 排序 而不是仅标识(支持 )。 .正如参考文章所说,如果你想要一个顺序,你必须自己强加。编译器无法为您推断出一个,也没有默认值 "reference comparison" 因为有默认值 "reference equality".
您可以应用 StructuralComparison
属性来要求类型确实实现了比较(类似于 : comparison
通用约束),这允许进行排序操作。这使编译器在编译时保证您的类型满足推断类型实现比较的要求。
那么,你是怎么做到的"reference comparison"?你不知道!正如文章所说,这没有意义。您需要使用已经实现比较的类型,或者例如为您的类型实施 IComparable<T>
,使其满足 comparison
约束。
如果您需要更多帮助,则需要更加具体。 Post 一个很好的问题 Minimal, Complete, and Verifiable code example 表明您 具体 遇到的困难 运行 ,尝试将参考文章中的建议应用到您的自己的代码,并准确解释您尝试过的内容,以及您在开始工作时仍然遇到的困难。
在Equality and Comparison Constraints in F#中表示:
Note there is no such thing as “reference comparison” (the object pointers used by .NET move around, so the ordering would change). You might implement that by using a unique tag and custom comparison.
如何在 F# 和 C# 中使用唯一标记进行比较?
How to do reference comparison in .NET? How can I use an unique tag for comparison in F# and C#?
请注意,在此上下文中,单词 "comparison" 与 "equality" 不同。它描述了 排序 而不是仅标识(支持 )。 .正如参考文章所说,如果你想要一个顺序,你必须自己强加。编译器无法为您推断出一个,也没有默认值 "reference comparison" 因为有默认值 "reference equality".
您可以应用 StructuralComparison
属性来要求类型确实实现了比较(类似于 : comparison
通用约束),这允许进行排序操作。这使编译器在编译时保证您的类型满足推断类型实现比较的要求。
那么,你是怎么做到的"reference comparison"?你不知道!正如文章所说,这没有意义。您需要使用已经实现比较的类型,或者例如为您的类型实施 IComparable<T>
,使其满足 comparison
约束。
如果您需要更多帮助,则需要更加具体。 Post 一个很好的问题 Minimal, Complete, and Verifiable code example 表明您 具体 遇到的困难 运行 ,尝试将参考文章中的建议应用到您的自己的代码,并准确解释您尝试过的内容,以及您在开始工作时仍然遇到的困难。