Skip to main content
Just use .Equals()... duh
Source Link
Jeroen Mostert
  • 29k
  • 2
  • 60
  • 92

The problem is here:

this.currentState as Enum == other.currentState as Enum 

Enum is a reference type, so your enum gets boxed into a (new, unique) object. As a result it no longer compares equal to any other boxed instance. Unfortunately  

enum instances don't implementtypes do the Right Thing for overriding IComparableEquals or, though IEquatable either(as @hvd so correctly points out), so it's not as simple as adding that as a constraint. You can however get a comparer for them:

Comparer<T> cmp = Comparer<T>.Default; 

And now you can just do

cmp.Compare(this.currentState, other.currentState) == 0 

You could also simply convert to long, since any enum is guaranteed to fit in that:

this.currentState.ToInt64Equals(null) == other.currentState.ToInt64(null) 

The problem is here:

this.currentState as Enum == other.currentState as Enum 

Enum is a reference type, so your enum gets boxed into a (new, unique) object. As a result it no longer compares equal to any other boxed instance. Unfortunately enum instances don't implement IComparable or IEquatable either, so it's not as simple as adding that as a constraint. You can however get a comparer for them:

Comparer<T> cmp = Comparer<T>.Default; 

And now you can do

cmp.Compare(this.currentState, other.currentState) == 0 

You could also simply convert to long, since any enum is guaranteed to fit in that:

this.currentState.ToInt64(null) == other.currentState.ToInt64(null) 

The problem is here:

this.currentState as Enum == other.currentState as Enum 

Enum is a reference type, so your enum gets boxed into a (new, unique) object. As a result it no longer compares equal to any other boxed instance. 

enum types do the Right Thing for overriding Equals, though (as @hvd so correctly points out), so you can just do

this.currentState.Equals(other.currentState) 
added 160 characters in body
Source Link
Jeroen Mostert
  • 29k
  • 2
  • 60
  • 92

The problem is here:

this.currentState as Enum == other.currentState as Enum 

Enum is a reference type, so your enum gets boxed into a (new, unique) object. As a result it no longer compares equal to any other boxed instance. Unfortunately enum instances don't implement IComparable or IEquatable either, so it's not as simple as adding that as a constraint. You can however get a comparer for them:

Comparer<T> cmp = Comparer<T>.Default; 

And now you can do

cmp.Compare(this.currentState, other.currentState) == 0 

You could also simply convert to long, since any enum is guaranteed to fit in that:

this.currentState.ToInt64(null) == other.currentState.ToInt64(null) 

The problem is here:

this.currentState as Enum == other.currentState as Enum 

Enum is a reference type, so your enum gets boxed into a (new, unique) object. As a result it no longer compares equal to any other boxed instance. Unfortunately enum instances don't implement IComparable or IEquatable either, so it's not as simple as adding that as a constraint. You can however get a comparer for them:

Comparer<T> cmp = Comparer<T>.Default; 

And now you can do

cmp.Compare(this.currentState, other.currentState) == 0 

The problem is here:

this.currentState as Enum == other.currentState as Enum 

Enum is a reference type, so your enum gets boxed into a (new, unique) object. As a result it no longer compares equal to any other boxed instance. Unfortunately enum instances don't implement IComparable or IEquatable either, so it's not as simple as adding that as a constraint. You can however get a comparer for them:

Comparer<T> cmp = Comparer<T>.Default; 

And now you can do

cmp.Compare(this.currentState, other.currentState) == 0 

You could also simply convert to long, since any enum is guaranteed to fit in that:

this.currentState.ToInt64(null) == other.currentState.ToInt64(null) 
Source Link
Jeroen Mostert
  • 29k
  • 2
  • 60
  • 92

The problem is here:

this.currentState as Enum == other.currentState as Enum 

Enum is a reference type, so your enum gets boxed into a (new, unique) object. As a result it no longer compares equal to any other boxed instance. Unfortunately enum instances don't implement IComparable or IEquatable either, so it's not as simple as adding that as a constraint. You can however get a comparer for them:

Comparer<T> cmp = Comparer<T>.Default; 

And now you can do

cmp.Compare(this.currentState, other.currentState) == 0