You're looking for generic constraints but you can't constrain a type parameter to only be valid for a specific set of types. The closest you could come would be something like:
public static decimal FindBestSubsequence<T>
(this IEnumerable<T> source, out int startIndex, out int endIndex)
where T : struct, IConvertible, IFormattable, IComparable<T>, IEquatable<T>,
IComparable
... as those are all interfaces which each of those types implements. However, this wouldn't prevent, say, Int16
from being used as the type argument. Do you definitely not want it to be applicable for an IEnumerable<short>
? What would go wrong if it were used for that?
You could have a set of non-generic public overloads, which then called to a constrained generic private method:
public static decimal FindBestSubsequence(this IEnumerable<decimal> source,
out int startIndex, out int endIndex)
{
return FindBestSubsequenceImpl(source, startIndex, endIndex);
}
public static decimal FindBestSubsequence(this IEnumerable<int> source,
out int startIndex, out int endIndex)
{
return FindBestSubsequenceImpl(source, startIndex, endIndex);
}
// etc
// Could constrain T more if it was useful in the method, but we know
// T will only be one of the types we want, because only this class can
// call this method
private static decimal FindBestSubsequence<T>
(IEnumerable<T> source, out int startIndex, out int endIndex)
where T : struct
{
}