generic NOT constraint where T : !IEnumerable
No - there's no such concept either in C# or in the CLR.
I found my self trying to implement the same case mentioned in the comments:
void doIt<T>(IEnumerable<T> what) { }
void doIt<T>(T whats) { }
I excepted the following code to reference the first method:
doIt(new List<T>());
But it actually references the second one.
One solution is to cast the argument like this:
doIt(new List<T>().AsEnumerable<T>());
The cast could be hidden by another overload:
void doIt<T>(List<T> whats) {
doIt(whats.AsEnumerable<T>());
}
As far as I know it is not possible to do that.
What you can do is some runtime checking:
public bool MyGenericMethod<T>()
{
// if (T is IEnumerable) // don't do this
if (typeof(T).GetInterface("IEnumerable") == null)
return false;
// ...
return true;
}