I am pretty close to understand Generics now (I think).
However, just figured that System.Enum is not easy to implement as a generic type.
I have this class:
<
You are trying to perform a reference comparison on a value type (struct), use Equals
for equality instead:
public Button<TEnum> GetButton(TEnum Identifier) {
var button = _buttons
.Where(b => EqualityComparer<TEnum>.Default.Equals(b.Identifier, Identifier))
.FirstOrDefault();
if (button == null)
Debug.Log("'" + GetType().Name + "' cannot return an <b>unregistered</b> '" + typeof(Button<TEnum>).Name + "' that listens to '" + typeof(TEnum).Name + "." + Identifier.ToString() + "'.");
return button;
}
The button.Identifier == Identifier
statement cannot be performed, because the ==
operator does not exist for structs. On a class it would have performed a reference comparison.
And as @JeppeStigNielsen noted in his answer, to prevent a boxing equality comparison, it is better to use the EqualityComparer<TEnum>.Default.Equals
method.
Instead of the impossible
button.Identifier == Identifier
you should use
EqualityComparer<TEnum>.Default.Equals(button.Identifier, Identifier)
This avoids boxing the value into an object
box (or IComparable
box).