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

 

使用可为空值的数据类型和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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值