以 ? 结尾的变量类型 [英] Variable type ending with ?

查看:20
本文介绍了以 ? 结尾的变量类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是什么?是什么意思:

public bool? Verbose { get; set; }

当应用于string?时,出现错误:

When applied to string?, there is an error:

类型 'string' 必须是不可为 null 的值类型,以便将其用作泛型类型或方法 'System.Nullable' 中的参数 'T'

The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable'

推荐答案

? 使您的不可空(值)类型可以为空.它不适用于 string,因为它是引用类型,因此默认情况下可以为 null.

? makes your non-nullable (value) types nullable. It doesn't work for string, as it is reference type and therefore nullable by default.

来自 MSDN,关于值类型:

与引用类型不同,值类型不能包含空值.但是,可为空类型功能确实允许值类型是赋值为 null.

Unlike reference types, a value type cannot contain the null value. However, the nullable types feature does allow for values types to be assigned to null.

? 基本上是 的简写可空的结构.

? is basically a shorthand for Nullable<T> structure.

如果您想了解更多信息,MSDN 有一篇很棒的文章关于这个话题.

If you want to know more, MSDN has a great article regarding this topic.

这篇关于以 ? 结尾的变量类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆