您的位置:首页 > 其它

使用可为空值的数据类型和System.Nullable(Of T)泛型类型

2011-12-30 13:05 561 查看

使用可为空值的数据类型和System.Nullable(Of T)泛型类型

另一个非常有趣的泛型类型是System.Nullable(Of T),它允许定义为可空值的数据类型。
CLR数据类型有一个固定的可用值范围。例如,可以为System.Boolean数据类型赋予集合{True,False}中的一个值。
然而,自从.NET 2.0发布以后,创建可为空值的数据类型成为了可能。简言之,可为空值的类型可以表示它的底层类型的所有值,加上一个空(也称为未定义)值。因此,如果将可为空值的类型声明为Boolean,则可将集合{True,False,Nothing}中的值赋予该类型。

Another very interesting generic class is System.Nullable(Of T), which allows you to define nullable data types. As you know, CLR data types have a fixed range of possible values. For example, the System.Boolean data type can be assigned a value from the
set {True, False}. However, since the release of .NET 2.0, it became possible to create nullable data types. Simply put, a nullable type can represent all the values of its underlying type, plus an empty (aka undefined) value. Thus, if we declare a nullable
Boolean, it could be assigned a value from the set {True, False, Nothing}.To define a nullable variable type, simply create a new Nullable(Of T) object and specify the type parameter.

Be aware, however, that the specified type must be a value type! If you attempt to create a nullable reference type (including Strings, which as you recall are classes and therefore belong to the reference type category), you are issued a compile-time
error. For example:

Sub Main()
' Define some local nullable types.
Dim nullableInt As New Nullable(Of Integer)()
Dim nullableDouble As New Nullable(Of Double)()
Dim nullableBool As New Nullable(Of Boolean)()
' Error! Strings are reference types!
Dim s As New Nullable(Of String)()
End Sub

Module Program

Class DatabaseReader
Public numericValue As Nullable(Of Integer) = Nothing
Public boolValue As Nullable(Of Boolean) = True
Public Function GetIntFromDatabase() As Nullable(Of Integer)
Return numericValue
End Function

Public Function GetBoolFromDatabase() As Nullable(Of Boolean)
Return boolValue
End Function
End Class
Sub Main()
Console.ForegroundColor = ConsoleColor.Yellow
Console.WriteLine("*********** Fun with Nullable ***********" & vbLf)

Dim dr As New DatabaseReader()

Dim i As Nullable(Of Integer) = dr.GetBoolFromDatabase()
If (i.HasValue) Then
Console.WriteLine("Value of 'i' is :{0}", i.Value)
Else
Console.WriteLine("Value of 'i' is undefined.")
End If

Dim b As Nullable(Of Boolean) = dr.GetBoolFromDatabase()
If (b.HasValue) Then
Console.WriteLine("Value of 'b' is :{0}", b.Value)
Else
Console.WriteLine("Value of 'b' is undefined.")
End If

End Sub

End Module

简便方法:

Module Program

Class DatabaseReader
Public numericValue As Integer?
Public boolValue As Boolean? = True
Public Function GetIntFromDatabase() As Integer?
Return numericValue
End Function

Public Function GetBoolFromDatabase() As Boolean?
Return boolValue
End Function
End Class
Sub Main()
Console.ForegroundColor = ConsoleColor.Yellow
Console.WriteLine("*********** Fun with Nullable ***********" & vbLf)

Dim dr As New DatabaseReader()

Dim i As Integer? = dr.GetBoolFromDatabase()
If (i.HasValue) Then
Console.WriteLine("Value of 'i' is :{0}", i.Value)
Else
Console.WriteLine("Value of 'i' is undefined.")
End If

Dim b As Boolean? = dr.GetBoolFromDatabase()
If (b.HasValue) Then
Console.WriteLine("Value of 'b' is :{0}", b.Value)
Else
Console.WriteLine("Value of 'b' is undefined.")
End If

End Sub

End Module
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐