枚举和IEquatable< T> [英] Enums and IEquatable<T>
问题描述
在重构一些使用泛型的代码时,这个让我难过...
假设我声明了一个枚举MyEnum {...}
为什么MyEnum没有实现IEquatable< MyEnum> ?
当然,我可以将MyEnum实例转换为int / short,无论是什么
(因为int实现了IEquatable< int>),但我不知道不喜欢这样做,因为它b / b
感觉有点凌乱,然后我推销那些知道
基础代表是什么的东西...... >
我错过了什么吗?这只是一个疏忽吗?
(对于ref,我使用IEquatable< T>以避免使用==因为运算符
不能在委托声明中表示) 。
Marc
" Marc Gravell" <毫安********** @ gmail.com>写道:
(对于ref,我使用IEquatable< T>以避免使用==因为运算符
无法在委托声明中表达)。
你的意思是作为泛型类型参数约束吗?考虑传递IEqualityComparer< T>类型的
参数。如果你需要
比较项目你的构造函数。您可以将其默认为EqualityComparer< T> .Default。那个
将查询IEquatable< T>和其他界面做所有真正的
工作,如果有必要,可以回到object.Equals()。
- Barry
-
http://barrkel.blogspot.com/
一个有用的提示,谢谢你。遗憾的是它会涉及一些黑客攻击以获得适合我的预期用途......哦......好吧......
我仍然可以'不过要认为enums应该实现这个,但是; -p
Marc
Marc,
|是否有充分的理由说明为什么MyEnum没有实现IEquatable< MyEnum> ?
我不知道,我认为他们应该...
除了其他评论,请考虑在
$中使用EqualityComparer(Of T) b $ b你的通用类/方法。类似于:
使用System.Collections.Generic;
static T Something< T>(T value1,T value2)
{
if(EqualityComparer< T> .Default.Equals(value1,value2))
返回value1;
else
返回值2;
}
-
希望这有帮助
Jay B. Harlow [MVP - Outlook]
..NET应用程序架构师,爱好者,&福音传教士
T.S.布拉德利 - http://www.tsbradley.net
" ; Marc Gravell <毫安********** @ gmail.com>在消息中写道
news:el **************** @ TK2MSFTNGP04.phx.gbl ...
|在重构一些使用泛型的代码的时候,这个让我难过...
|
|假设我声明一个枚举MyEnum {...}
|
|是否有充分的理由说明为什么MyEnum没有实现IEquatable< MyEnum> ?
|
|当然,我可以将MyEnum实例转换为int / short,无论是什么
| (因为int实现IEquatable< int>),但我不喜欢这样做,因为它
|感觉有点乱,然后我就知道那些知道
|的东西基础代表是...
|
|我错过了什么吗?这只是一个疏忽吗?
| (对于ref,我使用IEquatable< T>以避免使用==因为
运算符
|不能在委托声明中表达)。
|
| Marc
|
|
This one stumped me while refactoring some code to use generics...
Suppose I declare an enum MyEnum {...}
Is there a good reason why MyEnum doesn''t implement IEquatable<MyEnum> ?
Of course, I can cast a MyEnum instance down to the int / short whatever
(since int implements IEquatable<int>), but I don''t like doing that, as it
feels a bit messy, and I am then propegating the things that know what the
base represenation is...
Am I missing something? Is this just an oversight?
(For ref, I am using IEquatable<T> to avoid having to use == since operators
cannot be expressed in the delegate declaration).
Marc
"Marc Gravell" <ma**********@gmail.com> wrote:
(For ref, I am using IEquatable<T> to avoid having to use == since operators
cannot be expressed in the delegate declaration).
Do you mean as a generic type parameter constraint? Consider passing an
argument of type IEqualityComparer<T> to your constructor if you need to
compare items. You can default it to EqualityComparer<T>.Default. That
will query for IEquatable<T> and other interfaces to do all the real
work, falling back to object.Equals() if necessary.
-- Barry
--
http://barrkel.blogspot.com/
A useful tip, thankyou. A shame that it will involve a bit of hacking to get
it to fit my intended usage... oh well...
I still can''t help but think that enums should implement this, though ;-p
Marc
Marc,
| Is there a good reason why MyEnum doesn''t implement IEquatable<MyEnum> ?
I don''t know, I thought they should...
In addition to other comments, consider using EqualityComparer(Of T) within
your generic class/method. Something like:
using System.Collections.Generic;
static T Something<T>(T value1, T value2)
{
if (EqualityComparer<T>.Default.Equals(value1, value2))
return value1;
else
return value2;
}
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Marc Gravell" <ma**********@gmail.com> wrote in message
news:el****************@TK2MSFTNGP04.phx.gbl...
| This one stumped me while refactoring some code to use generics...
|
| Suppose I declare an enum MyEnum {...}
|
| Is there a good reason why MyEnum doesn''t implement IEquatable<MyEnum> ?
|
| Of course, I can cast a MyEnum instance down to the int / short whatever
| (since int implements IEquatable<int>), but I don''t like doing that, as it
| feels a bit messy, and I am then propegating the things that know what the
| base represenation is...
|
| Am I missing something? Is this just an oversight?
| (For ref, I am using IEquatable<T> to avoid having to use == since
operators
| cannot be expressed in the delegate declaration).
|
| Marc
|
|
这篇关于枚举和IEquatable< T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!