How to specify an object that is of one class but not a certain subclass?

class A
{

}

class B : A
{

}

void method(A that is not a B argument) {}
void generic_method(generic_class<A that is not a B> generic_argument) {}
void params_method(params A that is not a B[] params_arguments) {}

Is there any syntactical way to do this? i realize that i could just do

if(argument is B)
    throw new ArgumentException("argument cannot be a B", "argument");

at the beginning of the first method, and do that in a foreach for the second and third, but I'm wondering if there is some keyword or OOP concept that accomplishes this better.

Jon Skeet
people
quotationmark

So it sounds like you're trying to express the opposite of a constraint such as:

var Foo<T>() where T : SomeClass

That would constrain T to be SomeClass or a subclass1... but you're trying to make it explicitly not a T.

No, I'm afraid there's no such constraint in C#.


1 Well, modulo the assumption that SomeClass is a class to start with; it could be an interface, with the meaning you probably expect.

people

See more on this question at Stackoverflow