473,748 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<MyEn um> ?

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 12543
"Marc Gravell" <ma**********@g mail.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 IEqualityCompar er<T> to your constructor if you need to
compare items. You can default it to EqualityCompare r<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<MyEn um> ?
I don't know, I thought they should...
In addition to other comments, consider using EqualityCompare r(Of T) within
your generic class/method. Something like:

using System.Collecti ons.Generic;

static T Something<T>(T value1, T value2)
{
if (EqualityCompar er<T>.Default.E quals(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**********@g mail.com> wrote in message
news:el******** ********@TK2MSF TNGP04.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<MyEn um> ?
|
| 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 EqualityCompare r(Of T) within
your generic class/method. Something like:
using System.Collecti ons.Generic;

static T Something<T>(T value1, T value2)
{
if (EqualityCompar er<T>.Default.E quals(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
EqualityCompare r<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.c om...
| "Jay B. Harlow [MVP - Outlook]" <Ja************ @tsbradley.net> wrote:
|
| > In addition to other comments, consider using EqualityCompare r(Of T)
within
| > your generic class/method. Something like:
|
|
| > using System.Collecti ons.Generic;
| >
| > static T Something<T>(T value1, T value2)
| > {
| > if (EqualityCompar er<T>.Default.E quals(value1, value2))
|
| That's what I said!
|
| -- Barry
|
| --
| http://barrkel.blogspot.com/
May 29 '06 #6
This is all fine - and I agree that EqualityCompare r<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<MyEn um>?

Marc
May 30 '06 #7
"Marc Gravell" <ma**********@g mail.com> wrote:
This is all fine - and I agree that EqualityCompare r<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<MyEn um>?


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<MyEn um>?
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**********@g mail.com> wrote in message
news:ez******** ******@TK2MSFTN GP05.phx.gbl...
| This is all fine - and I agree that EqualityCompare r<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<MyEn um>?
|
| Marc
|
|
May 31 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
8216
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 I cast int to it, then it compiles, but if I have declared the enum's type as int, why do I need to cast int to the value?
1
4681
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 have a methods with signitures like the following... unsigned char someMethod(unsigned char blah, SOMESTRUCT* somestruct);
2
2433
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 does this give me? Thankoo
3
21664
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 method to execute my Equals code. What am I doing wrong? T is the following NameValue : IEqualityComparer<NameValue> { public string Text;
0
2439
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 you do this? The most natural approach would be :
5
2389
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 object of generic type T, that supports IEquatable<T>, and a method, DoComparisons(T obj) to compare the member object to the object passed in.
3
1430
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 integer), but integer is not a structure, so what is the function of the AS Structure here?
4
3274
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 variables I'd like to display a state. So I wanted to use maps for quick look-ups... anyway here's my code for filling and verifying the contents of my map of maps: include<map> #define MAX_LENGTH 80; void gen_enums(void) { char varname =...
3
1547
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 CompareTo is defined in the IComparabler<Tbut the Equals is not mentioned.
0
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9544
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.