C# 为什么我不能使用 System.ValueType 作为泛型约束?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1854625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:25:03  来源:igfitidea点击:

Why can't I use System.ValueType as a generics constraint?

c#generics

提问by zfedoran

  • Why can't I use a constraint of where T : System.ValueType?
  • Why does Microsoft prevent this type from being a constraint?
  • 为什么我不能使用 的约束 where T : System.ValueType
  • 为什么 Microsoft 阻止这种类型成为约束?


Example:

例子:

Why can't I do the following?

为什么我不能执行以下操作?

// Defined in a .Net class
public void bar<T>(T a) where T : ValueType {...}

// Defined in my class
public void foo<T>(T a) where T : ValueType 
{ bar<T>(a); }

What is the difference in using struct over ValueType?

在 ValueType 上使用 struct 有什么区别?

// Defined in my class
public void foo<T>(T a) where T : struct 
{ bar<T>(a); }

采纳答案by Jon Skeet

There are two differences between using

使用有两个区别

where T : struct

and

where T : ValueType
  • the latter would allow Tto be ValueTypeitself, which is a reference type.
  • the latter would also allow Tto be a nullable value type
  • 后者允许TValueType它自己,它是一个引用类型。
  • 后者也允许T是一个可为空的值类型

The first of these differences is almost never what you want. The second could occasionallybe useful; Nullable<T>is slightly odd in that it satisfies neither the where T : structnor where T : classconstraint.

这些差异中的第一个几乎永远不是您想要的。第二种可能偶尔有用;Nullable<T>是它满足无论是有点奇怪where T : struct,也不where T : class约束。

More useful would be the constraint

更有用的是约束

where T : struct, System.Enum

which is prohibited by C# for no good reason that I can tell. See my blog postand the Unconstrained Melody projectfor more on this.

这是 C# 禁止的,我可以说没有充分的理由。有关这方面的更多信息,请参阅我的博客文章不受约束的旋律项目

回答by Rex M

ValueType is not the base class of value types, it is simply a container for the value when it is boxed. Since it is a container class and not in any sort of hierarchy for the actual types you're wanting to use, it is not useful as a generic constraint.

ValueType 不是值类型的基类,它只是值被装箱时的容器。由于它是一个容器类,而不是您要使用的实际类型的任何类型的层次结构,因此它不能用作通用约束。

回答by bobbymcr

Using structas a generic constraint is functionally equivalent to a "ValueType" constraint. In .NET, a struct is a value type.

使用struct作为一个通用的约束功能上等同于一个“值类型”的约束。在 .NET 中,结构是值类型

回答by VoteCoffee

I think the example below covers a lot of use cases that one would expect from ValueType. The parameter type of T? allows nullables, and the type constraints restrict it to structs that implement IFormattable which is true for the common value types I can think of.

我认为下面的示例涵盖了人们期望从 ValueType 获得的许多用例。T的参数类型?允许空值,并且类型约束将其限制为实现 IFormattable 的结构,这对于我能想到的常见值类型来说是正确的。

public void foo<T>(T? a) where T : struct, IFormattable

Note that this allows types such as decimal, datetime, timespan.

请注意,这允许诸如十进制、日期时间、时间跨度之类的类型。

https://docs.microsoft.com/en-us/dotnet/api/system.iformattable?view=netcore-3.1

https://docs.microsoft.com/en-us/dotnet/api/system.iformattable?view=netcore-3.1