Code Focused

Experiencing Nothingness in C# and Visual Basic

Nothing and null are one and the same, except in the language of programming.

When engaging in cross-language development, it's important to worry about nothing – the Visual Basic Nothing keyword, that is, and how it varies from the C# null counterpart. When initializing objects and nullable value types to a "no data" state, the keywords are identical. But null and Nothing have syntax-specific uses that don't travel well between the two languages.

Consider the initialization of standard value types, such as ordinary integer variables. If you want to set an Integer value to its default starting point, and the zero key is broken on your keyboard, you would type the following Visual Basic statement:

' ----- This sets someValue to 0 (zero).
Dim someValue As Integer = Nothing

That statement brings discomfort to C# developers, because you can't assign null directly to a standard integer. Instead, C# uses the default operator to initialize such values:

// ----- This also sets someValue to 0 (zero).
int someValue = default(int);

The role of Nothing as an initializer makes for some interesting coding differences between the two languages. Consider the following Visual Basic statement and its supposed syntactic equivalent in C#:

' ----- The VB verison:
Dim result As String = (5 + Nothing).ToString()

// ----- The C# version:
string result = (5 + null).ToString();

Because Nothing acts as a value type initializer, the parenthesized expression will add five to zero (the default for an integer), eventually storing "5" in the result variable. But the C# code, empowered with that language's single-minded null keyword, behaves differently. Adding null to an int produces null, and the result variable ends up with a zero-length string.

Adding integers and nulls together isn't a typical development scenario, but caution is warranted in the more common practice of letting strings and null values play together. String variables are reference types, and C# treats them as pure reference types, properly differentiating between uninitialized string variables and zero-length strings:

string testValue = null;
if (testValue == "")
  result = "Empty";
else
  result = "Not Empty";

In that C# code, result will be set to "Not Empty," because the testValue null content isn't the same as a zero-length string. For historical reasons, Visual Basic treats null strings and zero-length strings as equivalent. At the end of the following code block, the Visual Basic result variable will contain "Empty," because the testValue comparison with the empty string results in True:

Dim testValue As String = Nothing
If (testValue = "") Then
  result = "Empty"
Else
  result = "Not Empty"
End If

To check for a true non-null, zero-length string in Visual Basic, you must first exclude null instances via the IsNot operator:

If (testValue IsNot Nothing) AndAlso (testValue.Length = 0) Then

It looks like extra work, and it is. But the flexibility of the Visual Basic Nothing keyword does result in simpler code when you want to treat a string as just a string, and not concern yourself with whether it can be null.

About the Author

Tim Patrick has spent more than thirty years as a software architect and developer. His two most recent books on .NET development -- Start-to-Finish Visual C# 2015, and Start-to-Finish Visual Basic 2015 -- are available from http://owanipress.com. He blogs regularly at http://wellreadman.com.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube