I was going to create an In extension method on int, when I realized, if I use IEquatable I’d get the extension on all primitive types. This is my current implementation
public static bool In<T>(this T value, params T[] values) where T : IEquatable<T>
{
return values.Contains(value);
}
It works just fine if(i.In(1,3,7)) for ints, but the where T: IEquatable<T> just looks wrong to me. Is this the correct way to define the generic method?
What you have looks right to me. You’re saying that the type must implement
IEquatable<T>for its own type.You could also extend it to say that the type you’re comparing against can be a different type that implements
IEquatable<T>for thevalue: