Visual Basic and C#: Differences in Nullable Types

| by Jonathan Allen Follow 576 Followers on Sep 17, 2007. Estimated reading time: 1 minute |

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.

Rate this Article

Adoption Stage

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.

Tell us what you think

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

Email me replies to any of my messages in this thread

I feel a new pattern coming on... by John DeHope

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.

nullable types in VB by totoro totoro

"VB, on the other hand has to somehow map its three-state logic to a Boolean value."

What? Jonathan please learn how VB works before posting some rubbish like this. Nothing WIDENS to ALL types in VB .Net. You can assign a plain integer to nothing. Nothing will widen to 0. Nothings also widens to a bool as...FALSE!

VB's implementation of nullable types actually makes sense; the null propagating is consistent throughout every operation. In C# and VB nullables propagate in +,-,/,*. In VB they also propagate the exact same way with comparisons. For some reason C# chose to not to propigate nullables. Paul Vick has a blog on this.

I'm always annoyed when I see a C person pretending to understand VB.

Re: nullable types in VB by Jonathan Allen

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

Email me replies to any of my messages in this thread

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

Email me replies to any of my messages in this thread

3 Discuss

Login to InfoQ to interact with what matters most to you.

Recover your password...


Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.


More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.


Stay up-to-date

Set up your notifications and don't miss out on content that matters to you