Enums and IEquatable<T>

M

Marc Gravell

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
 
B

Barry Kelly

Marc Gravell said:
(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
 
M

Marc Gravell

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
 
J

Jay B. Harlow [MVP - Outlook]

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


| 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
|
|
 
B

Barry Kelly

Jay B. Harlow said:
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
 
J

Jay B. Harlow [MVP - Outlook]

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


|
| > 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/
 
M

Marc Gravell

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
 
B

Barry Kelly

Marc Gravell said:
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
 
M

Marc Gravell

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
 
J

Jay B. Harlow [MVP - Outlook]

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/guide/language/enums.html

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| 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
|
|
 
J

Jon Skeet [C# MVP]

J

Jay B. Harlow [MVP - Outlook]

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


| > | 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/guide/language/enums.html
|
| Very much so. With partial classes, C# could make life even easier. See
| http://msmvps.com/blogs/jon.skeet/archive/2006/01/05/classenum.aspx for
| my take on things.
|
| --
| Jon Skeet - <[email protected]>
| http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
| If replying to the group, please do not mail me too
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top