Som Bhattacharyya Som Bhattacharyya - 6 months ago 25
Vb.net Question

Option type in VB

Is there a

Option
type that we can use in VB? I am working on a project in VB right now and am thinking of a way to get rid off all the
if
check on
null
/
Nothing
and returning of
null
/
Nothing
.
If i can have something of that sort i could statically ensure that a method returns an
option
instead of surprising callers by returning null .
That would make the code less error prone and would read so much better.

Answer

Short answer:

No.

Long answer:

Support for proper option types, or algebraic data types in general, isn't coming to C# / VB.Net any time soon.

Support for non-nullable types, which aren't quite the same but would still allow compile-time null-checking, is planned for the upcoming C# 7. AFAIK it has not been confirmed yet that VB 15 will include the same feature though.

You can come up with a number of anti-NullReferenceException tricks on your own which minimise the chance of accidentally passing a null around, but it's simply not possible to make them 100% compile-time safe for the time being.