473,386 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Enums and IEquatable<T>

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
11 12361
"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
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
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
"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
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
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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: SteveK | last post by:
I'm getting the error: "Cannot implicitly convert type 'MovesDBMigrate.MotionNameElementTypes' to 'int'" for this line of code: m_nameElementTableNames = "Tbl_NameCharacters"; Of course if...
1
by: | last post by:
Hi, Is there any good links for datatype interop? I need to pass some structure pointers into an unmanaged method and return char* etc but having some problems in my C++/CLI proxy class. I...
2
by: Chad | last post by:
I've used bitwise enums before (powers of 2) and have used AND and OR operator to determine which individual flags have been set, but I have never used the <Flags()> _ attribute What advantage...
3
by: JB | last post by:
I've created a generic of type List<T> where T is a custom class. I need to use the List<T>.Contains method. I know I need to implement the IEqualityComparer but I can't seem to get the Contains...
0
by: adebaene | last post by:
Hello all, Has everyone tried to use the functions taking a Predicate in Generics container in C++/CLI? Say I have a List<MyClass^>^ my_array, and I want to call RemoveAll on it. How would...
5
by: taumuon | last post by:
I've got an object, Person, that supports IEquatable<Person>. It implements bool Equals(Person obj) as well as overriding bool Equals(object obj) I've got a container type that holds a member...
3
by: =?Utf-8?B?Y2xhcmE=?= | last post by:
Hi all, how to understand the following declaration in MSDN about nullable structure <SerializableAttribute_ Public Structure Nullable(Of T As Structure) I can : dim id as nullable(of...
4
jlm699
by: jlm699 | last post by:
I've looked at the other articles about maps of maps and am still stuck on this! I'm trying to basically make an enumeration of a data monitoring app. Instead of displaying numbers for certain...
3
by: TonyJ | last post by:
Hello! I'm reading in a book from Microsoft that the generic interface IComparabler<Tdefine the two methods CompareTo and Equals. Now to my querstion if I look in the docs it says that...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.