Is it possible to specify a constraint on a generic class that disallows certain types? I don’t know if it is possible and if it is, I am not sure what the syntax would be. Something like:
public class Blah<T> where : !string {
}
I can’t seem to find any notation that would allow such a constraint.
The closest you can get is a run-time constraint.
Edit: Originally I put the run-time check in the constructor call. That’s actually not optimal, as it incurs overhead on every instantiation; I believe it would be much more sensible to put the check in the static constructor, which will be invoked once per type used as the
Tparameter for yourBlah<T>type:Obviously not ideal, but if you put this constraint in place and document the fact that
stringis not a supported type argument (e.g., via XML comments), you should get somewhere near the effectiveness of a compile-time constraint.