By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,956 Members | 1,221 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,956 IT Pros & Developers. It's quick & easy.

Enums and IEquatable<T>

P: n/a
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
May 25 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
"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/
May 25 '06 #2

P: n/a
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
May 25 '06 #3

P: n/a
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
|
|
May 29 '06 #4

P: n/a
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote:
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))


That's what I said!

-- Barry

--
http://barrkel.blogspot.com/
May 29 '06 #5

P: n/a
Barry,
Pardon me! I read your message that you were suggesting passing a parameter.

I was suggesting to forgo the parameter & simply use
EqualityComparer<T>.Default inline.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Barry Kelly" <ba***********@gmail.com> wrote in message
news:nq********************************@4ax.com...
| "Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote:
|
| > 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))
|
| That's what I said!
|
| -- Barry
|
| --
| http://barrkel.blogspot.com/
May 29 '06 #6

P: n/a
This is all fine - and I agree that EqualityComparer<T>.Default is better
than casting to the base type (int or what-have-you), but (back in
problem-town rather than
solution-ville) am I alone in thinking that an enum MyEnum should implement
IEquatable<MyEnum>?

Marc
May 30 '06 #7

P: n/a
"Marc Gravell" <ma**********@gmail.com> wrote:
This is all fine - and I agree that EqualityComparer<T>.Default is better
than casting to the base type (int or what-have-you), but (back in
problem-town rather than
solution-ville) am I alone in thinking that an enum MyEnum should implement
IEquatable<MyEnum>?


The only thing I can think of is the cost of instantiation: every time a
generic type is instantiated, either memory usage or executables get
bigger. Could be an issue for compact framework etc.

-- Barry

--
http://barrkel.blogspot.com/
May 30 '06 #8

P: n/a
I guess that makes some sense, especially when you consider how structs and
generics behave; and there's a lot of enums in the framework, so if each of
those declared their own IEquatable<some struct>...

Interesting theory - cheers,

Marc
May 30 '06 #9

P: n/a
Marc,
| am I alone in thinking that an enum MyEnum should implement
| IEquatable<MyEnum>?
No you are not alone, I think they should also!

Personally I find the news Java Enum to be "better" then the .NET enum...

http://java.sun.com/j2se/1.5.0/docs/...age/enums.html

--
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:ez**************@TK2MSFTNGP05.phx.gbl...
| This is all fine - and I agree that EqualityComparer<T>.Default is better
| than casting to the base type (int or what-have-you), but (back in
| problem-town rather than
| solution-ville) am I alone in thinking that an enum MyEnum should
implement
| IEquatable<MyEnum>?
|
| Marc
|
|
May 31 '06 #10

P: n/a
Jay B. Harlow [MVP - Outlook] <Ja************@tsbradley.net> wrote:
| am I alone in thinking that an enum MyEnum should implement
| IEquatable<MyEnum>?
No you are not alone, I think they should also!

Personally I find the news Java Enum to be "better" then the .NET enum...

http://java.sun.com/j2se/1.5.0/docs/...age/enums.html


Very much so. With partial classes, C# could make life even easier. See
http://msmvps.com/blogs/jon.skeet/ar...classenum.aspx for
my take on things.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 31 '06 #11

P: n/a
Jon,
Interesting reading as usual!

My initial thought was the base class for the new Enums would be Generic,
reading your article I'm not sure being generic would be a benefit... Being
generic would allow broad categorization of enums in that these are int
enums, these are string enums, these are char enums...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
| Jay B. Harlow [MVP - Outlook] <Ja************@tsbradley.net> wrote:
| > | am I alone in thinking that an enum MyEnum should implement
| > | IEquatable<MyEnum>?
| > No you are not alone, I think they should also!
| >
| > Personally I find the news Java Enum to be "better" then the .NET
enum...
| >
| > http://java.sun.com/j2se/1.5.0/docs/...age/enums.html
|
| Very much so. With partial classes, C# could make life even easier. See
| http://msmvps.com/blogs/jon.skeet/ar...classenum.aspx for
| my take on things.
|
| --
| Jon Skeet - <sk***@pobox.com>
| http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
| If replying to the group, please do not mail me too
May 31 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.