Nothing キーワード (Visual Basic)

任意のデータ型の既定値を表します。 参照型の場合、既定値は null 参照です。 値型の場合、既定値は、値型で null が許容されるかどうかによって異なります。

注意

null 非許容値型の場合、Visual Basic の Nothing は C# の null と異なります。 Visual Basic で、null 非許容値型の変数を Nothing に設定すると、変数は宣言された型の既定値に設定されます。 C# では、null 非許容値型の変数を null に割り当てると、コンパイル時エラーが発生します。

Remarks

Nothing は、データ型の既定値を表します。 既定値は、変数が値型であるか、参照型であるかによって異なります。

"値型" の変数には、その値が直接格納されます。 値型には、すべての数値データ型 (BooleanCharDate)、すべての構造体、およびすべての列挙型が含まれます。 "参照型" の変数では、オブジェクトのインスタンスへの参照がメモリに格納されます。 参照型には、クラス、配列、デリゲート、および文字列が含まれます。 詳細については、「 Value Types and Reference Types」を参照してください。

変数が値型の場合、Nothing の動作は、変数が null 許容データ型かどうかによって異なります。 null 許容値型を表すには、型名に ? 修飾子を追加します。 Nothing を null 許容変数に割り当てると、値が null に設定されます。 詳細と例については、「null 許容値型」を参照してください。

変数が null 非許容値型である場合は、その変数に Nothing を割り当てると、宣言された型の既定値に設定されます。 その型に変数メンバーが含まれている場合は、すべて既定値に設定されます。 次の例では、スカラー型について説明します。

Module Module1

    Sub Main()
        Dim ts As TestStruct
        Dim i As Integer
        Dim b As Boolean

        ' The following statement sets ts.Name to null and ts.Number to 0.
        ts = Nothing

        ' The following statements set i to 0 and b to False.
        i = Nothing
        b = Nothing

        Console.WriteLine($"ts.Name: {ts.Name}")
        Console.WriteLine($"ts.Number: {ts.Number}")
        Console.WriteLine($"i: {i}")
        Console.WriteLine($"b: {b}")

        Console.ReadKey()
    End Sub

    Public Structure TestStruct
        Public Name As String
        Public Number As Integer
    End Structure
End Module

変数が参照型の場合は、変数に Nothing を割り当てると、その変数の型の null 参照に設定されます。 null 参照に設定されている変数は、どのオブジェクトにも関連付けられません。 この動作を次の例で示します。

Module Module1

    Sub Main()

        Dim testObject As Object
        ' The following statement sets testObject so that it does not refer to
        ' any instance.
        testObject = Nothing

        Dim tc As New TestClass
        tc = Nothing
        ' The fields of tc cannot be accessed. The following statement causes 
        ' a NullReferenceException at run time. (Compare to the assignment of
        ' Nothing to structure ts in the previous example.)
        'Console.WriteLine(tc.Field1)

    End Sub

    Class TestClass
        Public Field1 As Integer
        ' . . .
    End Class
End Module

参照 (または null 許容値型) 変数が null かどうかを調べるには、常に Is Nothing または IsNot Nothing を使います。 = Nothing<> Nothing も使用しないでください。

Visual Basic の文字列の場合、空の文字列は Nothing と等しくなります。 したがって、"" = Nothing は true となります。 このため、文字列を操作するときは正しい比較を選ぶことが特に重要になります。 myString = NothingmyString <> Nothing は空ではない値が設定されているかどうかを示しますが、この目的には String.IsNullOrEmpty(myString) を使うことを強くお勧めします。 何らかの値 (空の文字列を含む) が設定されたかどうかを判断するには、Is NothingIsNot Nothing を使います。

次の例は、Is 演算子と IsNot 演算子を使用する比較を示しています。

Module Module1
    Sub Main()

        Dim testObject As Object
        testObject = Nothing
        Console.WriteLine(testObject Is Nothing)
        ' Output: True

        Dim tc As New TestClass
        tc = Nothing
        Console.WriteLine(tc IsNot Nothing)
        ' Output: False

        ' Declare a nullable value type.
        Dim n? As Integer
        Console.WriteLine(n Is Nothing)
        ' Output: True

        n = 4
        Console.WriteLine(n Is Nothing)
        ' Output: False

        n = Nothing
        Console.WriteLine(n IsNot Nothing)
        ' Output: False

        Console.ReadKey()
    End Sub

    Class TestClass
        Public Field1 As Integer
        Private field2 As Boolean
    End Class
End Module

As 句を使用せずに変数を宣言し、それを Nothing に設定すると、変数の型は Object になります。 たとえば、Dim something = Nothing などです。 この場合、Option Strict がオンで Option Infer がオフであれば、コンパイル時エラーが発生します。

オブジェクト変数に Nothing を割り当てると、オブジェクト インスタンスを参照しなくなります。 変数が以前にインスタンスを参照していた場合、その変数を Nothing に設定しても、インスタンス自体は終了しません。 インスタンスが終了し、そのインスタンスに関連付けられているメモリおよびシステム リソースが解放されるのは、ガベージ コレクター (GC) によってアクティブな参照が残っていないことが検出された後のみです。

Nothing は、初期化されていないバリアントまたは存在しないデータベース列を表す DBNull オブジェクトとは異なります。

関連項目