泛型类型实现中的值和引用类型

时间:2021-12-16 16:13:33

I searched about generic type in C# and I made this conclusion:

我搜索了C#中的泛型类型,我做出了这样的结论:

  1. All reference types are based on Class
  2. 所有参考类型均基于Class

  3. All value types are based on struct
  4. 所有值类型都基于struct

  5. The main differences between struct and class, apart the global differences between value and reference type, are :

    除了值和引用类型之间的全局差异之外,struct和class之间的主要区别是:

    • No inheritance in struct

      结构中没有继承

    • The struct can not contain an empty constructor(without arguments)

      结构不能包含空构造函数(不带参数)

  6. There are six basic implementations of a generic type:
  7. 泛型类型有六种基本实现:

  • Where T: class ==>the generic parameter must be a reference type

    其中T:class ==>泛型参数必须是引用类型

  • Where T:classA ==>the generic parameter must be an instance of the class classA

    其中T:classA ==>泛型参数必须是类classA的实例

  • Where T:InterfaceA ==> the generic parameter must implement the interface InterfaceA

    其中T:InterfaceA ==>泛型参数必须实现接口InterfaceA

  • Where T:New() ==> the generic parameter must be a class + have a default empty constructor

    其中T:New()==>泛型参数必须是类+具有默认的空构造函数

  • Where T:U ==> the generic parameter must be derived the class U or implement the interface U

    其中T:U ==>泛型参数必须从类U派生或实现接口U.

  • Where T: struct ==> the generic parameter must be a value type

    其中T:struct ==>泛型参数必须是值类型

So I need to know:

所以我需要知道:

  1. If my conclusion is correct?
  2. 如果我的结论是正确的?

  3. I can't understand the difference between :
  4. 我无法理解之间的区别:

  • where T: New() ==> class with empty constructor

    其中T:New()==>带有空构造函数的类

  • where T: class, New() ==> class with empty constructor

    其中T:class,New()==>带有空构造函数的类

Why the second form is used? Why we don't just use the first one?

为什么使用第二种形式?为什么我们不只是使用第一个?

Thanks,

3 个解决方案

#1


What you're describing are generic constraints.

您所描述的是通用约束。

Where T:New() ==> the generic parameter must be a class + have a default empty constructor

其中T:New()==>泛型参数必须是类+具有默认的空构造函数

No, that just says "the type argument must have a parameterless constructor". That actually includes all value types. Even though you couldn't declare your own parameterless constructors for structs before C# 6, you could always call them. For example:

不,那只是说“类型参数必须有一个无参数构造函数”。这实际上包括所有价值类型。即使您无法在C#6之前为结构声明自己的无参数构造函数,也可以随时调用它们。例如:

Guid guid = new Guid();

So if you have:

所以如果你有:

public void Foo<T>() where T : new()

it's perfectly valid to call

打电话完全有效

Foo<Guid>();

#2


The generic constraint new() means that the type has a parameterless constructor. That type could be either a struct or a class. structs cannot provide a custom parameterless constructor, that is because all structs have a parameterless constructor already provided for them, with a default behavior that they cannot change. It doesn't mean that structs can never be created with a parameterless constructor.

泛型约束new()表示该类型具有无参数构造函数。该类型可以是结构或类。结构不能提供自定义无参数构造函数,这是因为所有结构都有一个已经为它们提供的无参数构造函数,并且默认行为无法更改。这并不意味着永远不能使用无参数构造函数创建结构。

#3


The struct can not contain an empty constructor(without arguments).

结构不能包含空构造函数(不带参数)。

Not true. The struct will always have a parameterless constructor. You are not however allowed to change it from the default parameterless constructor that you get automatically.

不对。该结构将始终具有无参数构造函数。但是,您不能从自动获得的默认无参数构造函数中更改它。

#1


What you're describing are generic constraints.

您所描述的是通用约束。

Where T:New() ==> the generic parameter must be a class + have a default empty constructor

其中T:New()==>泛型参数必须是类+具有默认的空构造函数

No, that just says "the type argument must have a parameterless constructor". That actually includes all value types. Even though you couldn't declare your own parameterless constructors for structs before C# 6, you could always call them. For example:

不,那只是说“类型参数必须有一个无参数构造函数”。这实际上包括所有价值类型。即使您无法在C#6之前为结构声明自己的无参数构造函数,也可以随时调用它们。例如:

Guid guid = new Guid();

So if you have:

所以如果你有:

public void Foo<T>() where T : new()

it's perfectly valid to call

打电话完全有效

Foo<Guid>();

#2


The generic constraint new() means that the type has a parameterless constructor. That type could be either a struct or a class. structs cannot provide a custom parameterless constructor, that is because all structs have a parameterless constructor already provided for them, with a default behavior that they cannot change. It doesn't mean that structs can never be created with a parameterless constructor.

泛型约束new()表示该类型具有无参数构造函数。该类型可以是结构或类。结构不能提供自定义无参数构造函数,这是因为所有结构都有一个已经为它们提供的无参数构造函数,并且默认行为无法更改。这并不意味着永远不能使用无参数构造函数创建结构。

#3


The struct can not contain an empty constructor(without arguments).

结构不能包含空构造函数(不带参数)。

Not true. The struct will always have a parameterless constructor. You are not however allowed to change it from the default parameterless constructor that you get automatically.

不对。该结构将始终具有无参数构造函数。但是,您不能从自动获得的默认无参数构造函数中更改它。