简体   繁体   English

什么是 ( <type> ?)?

[英]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>缩写。

Nullable types in C# C#中的可空类型

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时使用此选项,例如intBooleanDateTime

它意味着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

As other people have said int? 正如其他人所说的那样int? is short for Nullable<int> . Nullable<int>缩写。

This article is a couple of years old now but it's a good explanation of nullable types 这篇文章现在已经有几年了,但它是对可空类型的一个很好的解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM