Nullable types in C# and VB.net

Have you ever try to assign null to int value in C# or VB

Both language has totally different behavior. VB allows you assign Nothing but it will translate to zero on the other end C# throws error “type is not nullable”

Here is how you can set Integer to Null value in Both languages

Visual basic.net

Dim i as Integer=Nothing  '//Works but set i=0
Dim i as Integer?=Nothing  '//Works set i=Nothing

C#

int i =null '//ERROR: type not nullable
int? i =null '//Works set i=null

Binary World is a Software Development company located in Atlanta, USA (since 2007). Binary World specialized in Business Intelligence, mobile, cloud computing and .Net Application Development.

Leave a Reply