I am trying to create a derived class from a generic class, and I was wondering what the differences are between
public class A<T> : B<T> where T : C
{
}
and
public class A: B<C>
{
}
Inside class A there probably will be no code, since (for now) it will not behave different from class B. I only want to distinguish the two classes.
Thanks in advance.
Say you had a class
public class D : C
{
}
Then in your first example the below is valid.
var a = new A<D>
You can use any class for T
that is ultimately derived from C.
Whereas your second code is hard coded to have B use C for the genric type parameter and is not generic.
It is a Constraints with generics in C#, for sample:
In
public class A<T> : B<T> where T : C
{
}
The generic T
must be a C
type or a child of it (what is a abstraction).
In
public class A: B<C>
{
}
The generic is C
.
In your first example, A
is a generic class, of type C
. It also inherits from class B
of type C
.
Your second example has the following properties:
A
is not a generic class. It inherits from class B
of type C
.
So, they are actually quite different.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With