我正在阅读我的新 MVC 书,当然,示例都像往常一样使用 c# 编写。
有一行代码说
public bool? WillAttend { get; set; }
作者解释说,问号表示这是一个可为真、假的可空(三态) bool 值。或空。 (新的 C# 3 约定。)
vb.net 是否支持任何这样的约定。当然我可以在 vb.net 中声明一个 bool 值,我可以明确地将它设置为 Null(在 vb.net 中没有)。
有什么不同。在 c# 中还有更多内容吗?好处?
最佳答案
bool?
只是速记syntax for a nullable value type : 即 Nullable<bool>
Boolean?
或 Nullable(Of Boolean)
. 你可以像这样写一个支持属性:
Private _willAttend As Nullable(Of Boolean)
Public Property WillAttend As Nullable(Of Boolean)
Get
Return _willAttend
End Get
Set(value As Nullable(Of Boolean))
_willAttend = value
End Set
End Property
或者只是使用 auto-implemented property像这样:
Public Property WillAttend As Boolean?
关于vb.net - vb.net 中是否有可空的 bool 之类的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1225471/