[英]What is (<type>?)?
I was looking through some code the other day and I saw something like (int?) and I dont think Ive ever see that before. 前几天我正在查看一些代码,我看到类似(int?)的东西,我不认为我以前曾经看到过。 What does it mean when you use a ?
你用的是什么意思? after a type?
一个类型?
It is short for Nullable<T>
. 它是
Nullable<T>
缩写。
This is a generic struct that can wrap a value-type to add the value null
. 这是一个通用结构,可以包装值类型以添加值
null
。 To make the use of this type more convenient C# adds quite a bit of compiler-magic. 为了更方便地使用这种类型,C#增加了相当多的编译器魔法。 Such as the short-name
T?
比如短名
T?
, lifted operators,... ,解除了运营商,......
The following thread on SO is interesting too: ? SO上的以下主题也很有趣: ? (nullable) operator in C#
C#中的(可空)运算符
It's a variation/alternative of the Nullable<Type>
. 它是
Nullable<Type>
的变体/替代品。 Have seen it used a lot with DateTime to avoid the default DateTime value which gives an error in DB columns related to dates. 已经看到它在DateTime中使用了很多,以避免默认的DateTime值,该值在与日期相关的DB列中给出错误。 Quite useful actually.
实际上非常有用。
int? 诠释? is syntactic sugar for
Nullable<int>
. 是
Nullable<int>
语法糖。
That is the shorthand syntax for Nullable<T>
(or in your case Nullable<int>
). 这是
Nullable<T>
的简写语法(或者在你的情况下为Nullable<int>
)。
This is used when you need value types to be null, such as int
, Boolean
and DateTime
. 当您需要值类型为null时使用此选项,例如
int
, Boolean
和DateTime
。
它意味着Nullable,所以我们的value-type变量可以为null
the ? 的? after the type implies that the type can have
null
value besides its normal values. 在类型之后暗示类型除了正常值之外可以具有
null
值。
I've seen the use mostly for database related types where you have Nullable
columns 我已经看到主要用于数据库相关类型,你有
Nullable
列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.