I want to explain my title of this question.
I have the base class
public class BaseClass { /* stuff */ }
And other two classes which inherits BaseClass
public class Test1: BaseClass { ... }
public class Test2: BaseClass { ... }
Ok then let assume with generic class (in my project it is very complex)
public GenericClass<T> : IBase<T> where T: BaseClass, Test1, Test2
Sometimes I need to use only BaseClass otherwise Test1 or Test2.
I have a function:
public int Create ( T obj){
if( obj is Test1) { return aManager.Create((Test1)obj); } // the cast is OK
else if(obj is Test2) { return bManager.Create((Test2)obj);} // error cast
}
I have also aManager.Create (Test1 obj) and bManager.Create(Test2 obj)
Why in the else row I have error in cast?
Where is my mistake?
PS: If I change the order between Test1 and Test2 then in first if will occur casting error and in else will be ok.
where T: BaseClass, Test1, Test2combines classes withAND, notOR, so your class has to satisfy all these constraints.Createmethodpublic int Create ( T obj){. You need to declare itpublic int Create (object obj){