" A类:B "和" class A: B, T: C "的区别是什么?

时间:2021-06-14 20:54:45

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.

在A类里面可能没有代码,因为(现在)它不会和b类不同。我只想区分这两个类。

Thanks in advance.

提前谢谢。

5 个解决方案

#1


8  

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.

你可以用任何一个T的类最终从C中得到。

Whereas your second code is hard coded to have B use C for the genric type parameter and is not generic.

而你的第二个代码是硬编码的,用C来表示genric类型参数,而不是通用的。

#2


2  

It is a Constraints with generics in C#, for sample:

它是c#中泛型的约束,样本:

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).

一般的T必须是C类型或它的子元素(什么是抽象)。

In

public class A: B<C>
{
}

The generic is C.

一般是C。

#3


2  

In your first example, A is a generic class, of type C. It also inherits from class B of type C.

在您的第一个示例中,A是类型C的泛型类,它也继承了C类的B类。

Your second example has the following properties:

您的第二个示例具有以下属性:

A is not a generic class. It inherits from class B of type C.

A不是泛型类。它继承了C类的B类。

So, they are actually quite different.

所以,它们实际上是完全不同的。

#4


1  

In the first example you are making the the A Class as a generic class that that T must be given when instantiating the class.

在第一个示例中,您将一个类作为一个泛型类,在实例化该类时必须给出它。

A<C> instance = new A<C>();

In the second example, The A Class is not a generic class, since when instantiating the A Class there is no need to declare the T since its done automatically behind the scenes based on the A Class declaration.

在第二个示例中,一个类不是泛型类,因为当实例化一个类时,没有必要声明T,因为它是根据类声明自动在幕后完成的。

A instance = new A();

Another difference

另一个区别

If you have the given hierarchical inheritance tree:

如果你有给定的层级继承树:

" A类:B "和" class A: B, T: C "的区别是什么?

At the first example, when you instantiate the A class, every one of the inherited class can be used as the T.

在第一个示例中,当您实例化一个类时,每个继承的类都可以用作T。

At the second example, you can Instantiate the A class and specify the C2 class, so only classes that inherit from C2 can be used insde the A class.

在第二个示例中,您可以实例化一个类并指定C2类,因此只有从C2继承的类可以在一个类中使用。

#5


0  

In second option you will end up with a non-generic class A, whereas in the First option you will have generic class A

在第二个选项中,您将使用非泛型类a,而在第一个选项中,您将拥有泛型类a。

#1


8  

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.

你可以用任何一个T的类最终从C中得到。

Whereas your second code is hard coded to have B use C for the genric type parameter and is not generic.

而你的第二个代码是硬编码的,用C来表示genric类型参数,而不是通用的。

#2


2  

It is a Constraints with generics in C#, for sample:

它是c#中泛型的约束,样本:

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).

一般的T必须是C类型或它的子元素(什么是抽象)。

In

public class A: B<C>
{
}

The generic is C.

一般是C。

#3


2  

In your first example, A is a generic class, of type C. It also inherits from class B of type C.

在您的第一个示例中,A是类型C的泛型类,它也继承了C类的B类。

Your second example has the following properties:

您的第二个示例具有以下属性:

A is not a generic class. It inherits from class B of type C.

A不是泛型类。它继承了C类的B类。

So, they are actually quite different.

所以,它们实际上是完全不同的。

#4


1  

In the first example you are making the the A Class as a generic class that that T must be given when instantiating the class.

在第一个示例中,您将一个类作为一个泛型类,在实例化该类时必须给出它。

A<C> instance = new A<C>();

In the second example, The A Class is not a generic class, since when instantiating the A Class there is no need to declare the T since its done automatically behind the scenes based on the A Class declaration.

在第二个示例中,一个类不是泛型类,因为当实例化一个类时,没有必要声明T,因为它是根据类声明自动在幕后完成的。

A instance = new A();

Another difference

另一个区别

If you have the given hierarchical inheritance tree:

如果你有给定的层级继承树:

" A类:B "和" class A: B, T: C "的区别是什么?

At the first example, when you instantiate the A class, every one of the inherited class can be used as the T.

在第一个示例中,当您实例化一个类时,每个继承的类都可以用作T。

At the second example, you can Instantiate the A class and specify the C2 class, so only classes that inherit from C2 can be used insde the A class.

在第二个示例中,您可以实例化一个类并指定C2类,因此只有从C2继承的类可以在一个类中使用。

#5


0  

In second option you will end up with a non-generic class A, whereas in the First option you will have generic class A

在第二个选项中,您将使用非泛型类a,而在第一个选项中,您将拥有泛型类a。