Facilitating the spread of knowledge and innovation in professional software development



Choose your language

InfoQ Homepage News Visual Basic and C#: Differences in Nullable Types

Visual Basic and C#: Differences in Nullable Types


When .NET 1.0 was released, VB and C# were very similar. But with the fourth version nearing release, the differences are really mounting. Today we look at the subtle differences in nullable types that can trip up developers.

Nullable types were introduced with .Net 2.0. Using generics, any value type can be wrapped in Nullable, allow it to have a null value. Previously this could only be done by creating a custom class or boxing the value and assigning it to a variable of type object.

C# immediately added language support for nullable types. VB, still dealing with the fallout from the VB to VB.Net transition, was unable to do likewise. Nullables can still be used, but only at the API level.

With version 9, Visual Basic will add full support for nullable types. It will use a syntax nearly identical to C#, but with very different semantics. With C#, comparison operations always returns a true or false. In VB, nulls are propagated and thus a comparison may return true, false, or null.

 a=null, b=null
Operator C# Result  VB Result 
 == true   Nothing
 !=  false  Nothing
 >  false  Nothing
 <  false  Nothing
 >=  false  Nothing
 <=  false  Nothing

 a=1, b=null
Operator C# Result  VB Result 
 == false  Nothing
 !=  true  Nothing
 >  false  Nothing
 <  false  Nothing
 >=  false  Nothing
 <=  false  Nothing

These tables show an interesting anomaly in C#. While a==b returns true when both are null, a>=b and a<=b return false.

When it comes to checks requiring Booleans, C# is clear. VB, on the other hand has to somehow map its three-state logic to a Boolean value. It does this by equating null with false for the purpose of Boolean checks like If, While, and Until. This can lead to surprising results of its own.

a = null, b = null 
If a=b Then
'this line is executed
End if

If Not (a=b) Then
'this line is executed
End if

Both (a=b) and Not (a=b) return a null, and thus are considered to be false.

Knowing these differences and inconsistencies by heart is essential for developers working with nullable types in either language, lest subtle bugs be introduced.

We need your feedback

How might we improve InfoQ for you

Thank you for being an InfoQ reader.

Each year, we seek feedback from our readers to help us improve InfoQ. Would you mind spending 2 minutes to share your feedback in our short survey? Your feedback will directly help us continually evolve how we support you.

Take the Survey

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • I feel a new pattern coming on...

    by John DeHope,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    The first thing this makes me think, in VB.Net I'd probably start to write code like c# pseudocode...
    switch ( <possibly null boolean expression> )
    // code to handle null outcome here
    // code for handling true
    // code for handling false

    Really this just enforces in my mind how much we need a way to say a reference cannot be allowed to be null. They added a way to make a structures nullable, but not a way to make objects not nullable. I'd like to normalize the behavior. Today we say that "Dim i As Integer" means that i must always hold an integer value, never null. But then we go around and say "Dim s As String" allows s to be null or to have a value. This is annoying! I think we need it to be one way for both structures and objects, and we need the same syntax for declaring references that cannot be null. Something like...

    Dim i as integer // i cannot be null
    Dim i as integer? // i might be null
    Dim s as string // s cannot be null
    Dim s as string? // s might be null

    Same assumptions, same syntax.

  • Re: nullable types in VB

    by Jonathan Allen,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    It is important to understand that Nothing is a keyword, not a value. The Nothing you assign to an Integer is not the same Nothing you assign to an Integer?.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p


Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.