Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8577159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:05:52+00:00 2026-06-11T20:05:52+00:00

Possible Duplicate: When are two enums equal in C#? I have the following classes

  • 0

Possible Duplicate:
When are two enums equal in C#?

I have the following classes as part of a simple state machine.

Please note that all generic type parameters HAVE to be an enumeration. That has been enforced in the constructors (not shown here).

// Both [TState] and [TCommand] will ALWAYS be enumerations.
public class Transitions<TState, TCommand>: List<Transition<TState, TCommand>>
{
    public new void Add (Transition<TState, TCommand> item)
    {
        if (this.Contains(item))
            throw (new InvalidOperationException("This transition already exists."));
        else
            this.Add(item);
    }
}

// Both [TState] and [TCommand] will ALWAYS be enumerations.
public class Transition<TState, TCommand>
{
    TState From = default(TState);
    TState To  = default(TState);
    TCommand Command = default(TCommand);
}

public sealed class TransitionComparer<TState>:
    IComparer<TState>
{
    public int Compare (TState x, TState y)
    {
        int result = 0;

        // How to compare here since TState is not strongly typed and is an enum?
        //    Using ToString seems silly here.

        result |= x.From.ToString().CompareTo(y.From.ToString());
        result |= x.To.ToString().CompareTo(y.To.ToString());
        result |= x.Command.ToString().CompareTo(y.Command.ToString());

        return (result);
    }
}

The above does compile but I’m not sure if this is the right way to handle enums that have been passed in as generic type parameters.

Note: The compare function does not need to keep ordering in mind. Rather it needs to check for exact duplicates.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:05:53+00:00Added an answer on June 11, 2026 at 8:05 pm

    Note: The compare function does not need to keep ordering in mind. Rather it needs to check for exact duplicates.

    In that case, you shouldn’t be implementing IComparer<T>. You should be implementing IEqualityComparer<T> – or, more simply, make Transition implement IEquatable<Transition>.

    Note that you’re currently not using the TransitionComparer in the rest of the code, as far as we can see. It sounds like you shouldn’t really need to write your own comparison code for each enum value – you’re just trying to combine them. Unfortunately enums don’t implement IEquatable<T>, which makes is a bit harder to do this without boxing – how performance critical is this?

    Here’s a sample equality implementation for Transition:

    public class Transition<TState, TCommand>
        : IEquatable<Transition<TState, TCommand>>
    {
        // I assume in reality these are properties?
        TState From = default(TState);
        TState To  = default(TState);
        TCommand Command = default(TCommand);
    
        public override bool Equals(object other)
        {
            return Equals(other as Transition<TState, TCommand>);
        }
    
        public bool Equals(Transition<TState, TCommand> other)
        {
            if (other == null)
            {
                return false;
            }
            return From.Equals(other.From) &&
                   To.Equals(other.To) &&
                   Command.Equals(other.Command);
        }
    
        public int GetHashCode()
        {
            int hash = 17;
            hash = hash * 31 + From.GetHashCode();
            hash = hash * 31 + To.GetHashCode();
            hash = hash * 31 + Command.GetHashCode();
            return hash;
        }
    }
    

    EDIT: To avoid boxing for equality, I suspect you would either need some kind of delegate to get at the underlying value (and if you need to support values with different underlying types, that’s pretty painful) or potentially use something like Unconstrained Melody which uses IL rewriting to enforce the constraint at compile time, and allows you to check for equality based on underlying value more efficiently.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: CSS Selector that applies to elements with two classes I've got the
Possible Duplicate: JSON serialization of c# enum as string I have two classes as
Possible Duplicate: extending from two classes I'm currently trying to make a simple Mod
Possible Duplicate: combining two lamba expressions in c# I have two following expressions: Expression<Func<string,
Possible Duplicate: How can I have references between two classes in Objective-C? Objective-C #import
Possible Duplicate: Split into two variables? I have a variable that will output two
Possible Duplicate: Merge two rows in SQL I have the following values in table:
Possible Duplicate: Combine two selectors with one jQuery object I have the following jquery
Possible Duplicate: Concatenate Two NSDate String values Just confused that is there any default
Possible duplicate : comparing-two-arrays I have two NSArray and I'd like to create a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.