gpt4 book ai didi

c# - 运算符 '==' 不能应用于类型 T?

转载 作者:可可西里 更新时间:2023-11-01 08:17:27 24 4
gpt4 key购买 nike

我认为这个方法是有效的,但我错了:

static void Equals<T>(T x, T y)
{
return x == y; //operator == can't be applied to type T
}

阅读规范后(v3.0 中的 §7.2.4 和 v4.0 中的 §7.3.4):

7.2.4 Binary operator overload resolution

An operation of the form x op y, where op is an overloadable binary operator, x is an expression of type X, and y is an expression of type Y, is processed as follows:

  • The set of candidate user-defined operators provided by X and Y for the operation operator op(x, y) is determined. The set consists of the union of the candidate operators provided by X and the candidate operators provided by Y, each determined using the rules of §7.2.5. If X and Y are the same type, or if X and Y are derived from a common base type, then shared candidate operators only occur in the combined set once.

  • If the set of candidate user-defined operators is not empty, then this becomes the set of candidate operators for the operation. Otherwise, the predefined binary operator op implementations, including their lifted forms, become the set of candidate operators for the operation. The predefined implementations of a given operator are specified in the description of the operator (§7.7 through §7.11).

  • The overload resolution rules of §7.4.3 are applied to the set of candidate operators to select the best operator with respect to the argument list (x, y), and this operator becomes the result of the overload resolution process. If overload resolution fails to select a single best operator, a compile-time error occurs.

在第 2 步中,我认为应该应用这个预定义的实现:

bool operator ==(object x, object y);
bool operator !=(object x, object y);

因为 C# 中的所有内容都派生自 Object。第3步怎么会出现编译时错误呢?我认为在这种情况下“重载解析无法选择”是不可能的。

编辑当我实现这样的事情时,我想到了这个问题:

class EnumComparer<TEnum> : IEqualityComparer<TEnum>
{
public bool Equals(TEnum x, TEnum y)
{
return x == y;
}
public int GetHashCode(TEnum obj)
{
return (int)obj;
}
}

恐怕我需要构建一个表达式并在 Equals 方法中动态调用它。

最佳答案

很适合您阅读规范,但您很快就停止阅读了。如果你进一步阅读,你会得到这一点:


The predefined reference type equality operators require one of the following:

  • Both operands are a value of a type known to be a reference-type or the literal null. Furthermore, an explicit reference conversion exists from the type of either operand to the type of the other operand.

  • One operand is a value of type T where T is a type-parameter and the other operand is the literal null. Furthermore T does not have the value type constraint.

Unless one of these conditions are true, a binding-time error occurs. (*)


错误不是来自重载决策;错误是重载决策会选择预定义的引用类型相等运算符,而您没有引用类型。

考虑您的代码。是什么阻止 T 成为没有定义相等运算符的值类型?没有什么。假设我们退回到对象版本;两个操作数将装箱到不同的位置,因此引用不相等,即使它们具有相同的内容。由于那是缓慢的、困惑的和错误的,所以即使尝试也是非法的。

您最初为什么要尝试做这件事?如果你的方法有效,但它没有,那么你的方法将比简单地首先使用 == 更糟糕。您打算通过这种方法为世界增加什么值(value)?


(*) 我已经向规范维护者报告了这句话中的语法错误。

关于c# - 运算符 '==' 不能应用于类型 T?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5808057/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com