Assume there's a base class like this called a Cup
:
public abstract class Cup { }
And let's assume there're PaperCup
inheriting Cup
and PlasticCup
that inherits the PaperCup
class.
public class PlasticCup : PaperCup { }
public class PaperCup : Cup { }
And assume that there're two methods in Main class.
static void Test01 (PaperCup cup) { }
static void Test02 (Cup cup) { }
TEST1
PaperCup A = new PaperCup();
Test01(A);
Test02(A);
Above code works fine. A
instance can be passed into those two function because it is PaperCup
itself and it inheris Cup
base class.
TEST2
PlasticCup B = new PlasticCup();
Test01(B);
Test02(B);
Above code still works fine. B
instance also is able to be taken by the functions although it is PlasticCup
but it inheris PaperCup
and it is eventually derived from Cup
.
But Generics !!
Let's see following methods.
static void Test010 (IList<PaperCup> cup) { }
static void Test011(IList<PlasticCup> cup) { }
static void Test012(IList<Cup> cup) { }
And this trial below will fail at two method calls.
IList<PlasticCup> BB = new List<PlasticCup>();
Test010(BB); // Fail CS1503 Compiler Error
Test011(BB);
Test012(BB); // Fail CS1503 Compiler Error
Simple Question
Why is it impossible to pass the derived types into those functions when they take Generic
? isn't it supposed to work because C# is an OOP language?