Use of IsNothing() function in VB.NET

This function determines whether the expression passed as its argument evaluates to Nothing. It is a simple library function (comprising just 4 CIL instructions) which can itself be written in Visual Basic.
  • 7261

IsNothing() isn't inlined into a simple check for null. It's a function call. That will check for null. I was baffled when I found this. It returns True if the expression represents an object variable that currently has no object assigned to it; otherwise, it returns False. This function determines whether the expression passed as its argument evaluates to Nothing. It is a simple library function (comprising just 4 CIL instructions) which can itself be written in Visual Basic. A value type cannot hold a value of Nothing and reverts to its default value if you assign Nothing to it. If you supply a value type in Expression, IsNothing always returns False. 

Example: 

Public
 Class Sample
    
Public Shared Sub Main()
        
Dim Data1 As String = Nothing
        
Dim Output As New System.Text.StringBuilder

        
Output.AppendLine(String.Format( _
              
"IsNothing({0}) ... {1}", Data1, IsNothing(Data1)))
        
Output.AppendLine()

        
Data1 = "-11.1231"
        
Output.AppendLine(String.Format( _
           
"IsNothing({0}) ... {1}", Data1, IsNothing(Data1)))
        
Output.AppendLine()

        
Data1 = "October 21, 1987"
        
Output.Append(String.Format( _
           
"IsNothing({0}) ... {1}", Data1, IsNothing(Data1)))

        Console.WriteLine(Output.ToString())
        
Console.ReadLine()

    
End Sub

End
 Class

Output:

noyhing.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.