- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在研究接口(interface)的类型,但我不明白如何使用 IEquatable 接口(interface)。
我认为它比直接使用a.Equals(b)提供了更好的性能,因为我们避免了拳击...我已经这样做了:
public interface IEquatable<T> { bool Equals(T other); }
class Test<T> where T:IEquatable<T>
{
public static bool IsEqual(T a, T b) { return a.Equals(b); }
}
但是当我要调用时,我在编译中遇到错误,我不太确定我是否正确调用了该方法:
int x = 2;
int y = 2;
Console.WriteLine(Test.IsEqual(x, y));
错误是:
Error CS0305 Using the generic type 'Test' requires 1 type arguments
编辑:我不太确定这段代码,但它有效:
class Test<T> where T:IEquatable<T>
{
public static bool Equals(T a, T b)
{
return a.Equals(b);
}
}
class Program
{
static void Main(string[] args)
{
int x = 2;
int y = 2;
bool check = Test<int>.Equals(x, y);
Console.WriteLine(check);
Console.ReadKey();
}
}
何时必须使用此代码?,我在《C#6 Nutshell O'reilly》一书中读到了此内容
最佳答案
Test
不是东西 - 只有 Test<T>
。您可以做的第一件事就是使类型成为非通用的,并且方法通用:
class Test
{
public static bool IsEqual<T>(T a, T b)
where T : IEquatable<T>
{ return a.Equals(b); }
}
请注意,这仍然不好 - 它不能像 a
那样正确地工作 null
,但是......这并不重要,因为它仍然不会帮助你,因为 int
不实际上并没有实现你的 IEquatable<T>
。仅仅看起来正确的形状是不够的——它必须正式实现接口(interface)。幸运的是, int
确实为 System.IEquatable<T>
实现了内置 T==int
,因此只需完全删除接口(interface)定义即可。
但是,您在这里所做的一切都由 EqualityComparer<T>.Default
完成得更好。我建议:
class Test
{
public static bool IsEqual<T>(T a, T b)
=> EqualityComparer<T>.Default.Equals(a,b);
}
(请注意,您不需要通用约束 - 它仍然可以正常工作,使用 IEquatable<T>
可用时,否则使用 object.Equals
- 还考虑 null
、 Nullable<T>
等)。
注意:如果您确实在这里只使用int
,则应该只使用==
:
Console.WriteLine(x == y);
当您只知道 T
时,应该使用通用的相等方法,其中调用者提供 T
。
关于c# - 如何使用 IEquatable 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44328595/
我有一个树型类 Foo及其界面 IFoo . 我想要 Foo能够实现IEquatable>什么时候T : IEquatable (或者更一般地说,如果 T : I 我可能想要 Foo : I> 除了其
域类 T可以是 ValueObject 类型: public class Coordinate: ValueObject { ... } ValueObject实现 IEquatable界面。我想要
我首先使用带有 EntityFramework 代码的 ASP.NET MVC。 我正在尝试为将保存到我的数据库中的所有对象创建一个基类(这里称为实体类)。 有没有在基类中实现IEquatable的好
我想比较两个相同类型的自定义类对象。被比较的自定义类有一个 List 属性,其中填充了另一种自定义类型的项目。这可以通过继承 IEquatable 实现吗? 我不知道如何通过修改 MSDN's cod
当我使用 IEquatable interface 实现要比较的对象时: 为什么我必须覆盖 Equals(object)方法,如果我已经实现 Equals(T) ? 我可以使用==吗?和 !=运营商一
我想要一个通用类来实现 IEquatable 接口(interface)。该类具有 T 类型的数据项。要使泛型类等同,数据项也必须是 T 类型。 这是我的通用类: public class V : I
我有一个代表外部物理测量设备的类。简化版本如下所示: public class Device { public string Tag { get; set; } public int
我有一个 IEquatable方法如下: public bool Equals(TravelOptions other) { if (other == null) return false;
我有一个来自 SQL Server 数据库的 POCO,它有一个身份 ID 字段。我想实现 IEquatable 这样我就可以检查它们是否是相同的记录,使用 .Contains()在 List等 假设
我尝试使用以下方法来阻止人们更改 querystring为了查看其他人的详细信息: public static bool IsCurrentUserAuthorisedVessel(HttpReque
假设我有以下类(class): public sealed class ScaleValue : IComparable, IComparable, IEquatable { publ
我有一个可以毫无问题地序列化为 XML 的对象。但是,当我实现 IEquatable 并覆盖函数时,我收到有关未正确生成 XML 的错误。以前有人遇到过这个吗? 最佳答案 您可能需要覆盖 Equals
这是一个关于我正在尝试做的事情的例子: public class Foo : IEquatable { public bool Equals(Foo other) {
我经常使用代表工厂生产的实体的类。为了方便地测试我的工厂,我通常实现 IEquatable , 同时也覆盖 GetHashCode和 Equals (如 MSDN 所建议)。 例如;采用以下为示例目的
我有一个包含两个字符串和一个枚举的类。我正在尝试使用此类的实例作为字典中的键。不幸的是,我似乎没有正确实现 IEquatable。这是我的做法: public enum CoinSide {
我想知道 F# 中的相等测试在哪些情况下会导致装箱,以及是否存在覆盖 Equals 的情况。和 GetHashCode并实现IEquatable<>比使用 StructuralEqualityAttr
我刚刚将 Visual Studio 更新到最新的 16.6.0 Preview 1.0 和最新的 .NET Core 3.1.2。我的项目有enable . IEquatable 似乎发生了变化。现
我已经实现了IEquatable比较两个列表中的对象但是我想有条件地这样做: public bool Equals(CustomerType other) { if (this.Zipcode
我正在研究接口(interface)的类型,但我不明白如何使用 IEquatable 接口(interface)。 我认为它比直接使用a.Equals(b)提供了更好的性能,因为我们避免了拳击...我
嗯,我遇到了 IEquatable (C#) 的问题。正如您在下面的代码中看到的,我得到了一个实现了 IEquatable 的类,但是它的“Equals”方法无法实现。我的目标是:我的数据库中有一个日
我是一名优秀的程序员,十分优秀!