c#比较两个泛型值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/488250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# compare two generic values
提问by bernhardrusch
Possible Duplicate:
Can't operator == be applied to generic types in C#?
可能的重复:
运算符 == 不能应用于 C# 中的泛型类型吗?
I've coded something like this:
我已经编写了这样的代码:
public bool IsDataChanged()
{
T value1 = GetValue2;
T value2 = GetValue1();
return (valueInDB != valueFromView);
}
Right now the function doesn't compile with the error "Operator '!=' cannot be applied to operands of type 'T' and 'T'". What do I have to do to make this function work ?
现在该函数无法编译并出现错误“运算符 '!=' 不能应用于类型为 'T' 和 'T' 的操作数”。我该怎么做才能使此功能正常工作?
采纳答案by ShuggyCoUk
You cannot use operators on generic types (except for foo == null which is special cased) unless you add where T : class to indicate it is a reference type (then foo == bar is legal)
您不能在泛型类型上使用运算符(除了 foo == null 这是特殊情况),除非您添加 where T : class 以指示它是引用类型(然后 foo == bar 是合法的)
Use EqualityComparer<T>
.Default to do it for you. This will notwork on types which only supply an operator overload for == without also either:
使用EqualityComparer<T>
.Default 为你做这件事。这不适用于只为 == 提供运算符重载的类型,而没有:
- implement
IEquatable<T>
- overrides object.Equals()
- 实施
IEquatable<T>
- 覆盖 object.Equals()
In general implementing the == operator and not also doing at least one of these would be a very bad idea anyway so this is not likely to be an issue.
一般来说,实现 == 运算符而不至少执行其中一个操作将是一个非常糟糕的主意,因此这不太可能成为问题。
public bool IsDataChanged<T>()
{
T value1 = GetValue2;
T value2 = GetValue1();
return !EqualityComparer<T>.Default.Equals(value1 , value2);
}
If you do not restrict to IEquatable<T>
then the EqualityComparer default fallback may cause boxing when used with value types if they do not implement IEquatable<T>
(if you control the types which are being used this may not matter). I am assuming you were using =! for performance though so restricting to the Generic type will avoid accidentalboxing via the Object.Equals(object) route.
如果您不限制为,IEquatable<T>
那么 EqualityComparer 默认回退可能会在与未实现的值类型一起使用时导致装箱IEquatable<T>
(如果您控制正在使用的类型,这可能无关紧要)。我假设您正在使用 =!为了提高性能,因此限制为 Generic 类型将避免通过 Object.Equals(object) 路由意外装箱。
回答by CalvinR
That should work for you.
那应该对你有用。
public bool test<T>(T test, T test2) where T : class
{
return (test != test2);
}
This is simply pasted from the examples that were commented on your question.
这是从对您的问题发表评论的示例中简单粘贴的。
回答by Jay S
You can overload the .Equals() method on your objects and change your evaluation to:
您可以在对象上重载 .Equals() 方法并将评估更改为:
return (!valueInDB.Equals(valueFromView));
Assuming that valueInDB and valueFromView are objects. Your example variables aren't named the same as those used in the compare, so I had to assume.
假设 valueInDB 和 valueFromView 是对象。您的示例变量的名称与比较中使用的变量名称不同,因此我不得不假设。
EDIT: Got beat by 3 seconds! A note on overloading, if you need to compare values within a type, the basic .Equals() from the Object class won't be enough, as it will only do a memory compare for complex types. You'll need to overload and provide the implementation of how you want the object to be compared.
编辑:被击败了 3 秒!关于重载的注意事项,如果您需要比较一个类型中的值,来自 Object 类的基本 .Equals() 是不够的,因为它只会对复杂类型进行内存比较。您需要重载并提供您希望如何比较对象的实现。
回答by devio
Your type needs to implement the IComparableor IEquatableinterface.
您的类型需要实现IComparable或IEquatable接口。
Probably you then need to rewrite a!=b as !(a==b), or call the CompareTo() or Equals() method explicitly.
可能您随后需要将 a!=b 重写为 !(a==b),或者显式调用 CompareTo() 或 Equals() 方法。