When Fibonacci introduced the number zero to Europe, it must have blown their minds. People in the Fertile Crescent had been using zeros, probably for centuries, but for the people of this continent the concept of a symbol that meant 'nothing' was probably hard to grasp.
Enter the modern computer, which has the capacity to bewilder me on a daily basis. It can not only calculate numbers on both sides of 'nothing', but it also has many wholly separate ways of expressing 'nothing', none of which are compatible with each other. So for me, a hobbyist C programmer in my distant past, grappling with .NET's multiple ways of expressing 'nothing' was akin to the feeling that ancient people had when a mathematician attempted to express the idea of zero to civilizations used to counting in Roman numerals.
For instance, thanks to implicit type conversions in the C compiler (at least the one I was using), you could do this:
int i=0;
if (!i) printf("true");
if (i==0) printf("true");
if (i==NULL) printf("true");
This logic does not compute in .NET, however. I first came across this when trying to integrate an ActiveX web browser into a Windows Forms application a few years ago. All that I wanted to do was a simple navigation:
axWebBrowser1.Navigate("about:blank",null, null, null,null);
The last four arguments to the Navigate method were not important to me. I only wanted to make a GET request for "about:blank", but the line above caused a compiler error: Cannot convert from '<null>' to 'ref object'. I need to pass a ref object, which is clearly 'something', but it must have a value of 'nothing'! After pondering this for a bit, it was necessary to lie on the sofa with a damp washcloth on my forehead and turn out all the lights to soothe my aching head.
After a recovery period and a beer, I beset Google to try to find the answer and uncovered some information about the trinity of 'nothings' in .NET: Missing, Nothing (null), and Zero.
Nothing seems to be imbued with magickal powers that can cause real objects to be disposed of by the runtime. For instance, a static object will never be garbage collected unless its' value is set to null, or Nothing in VB .NET. Missing is a special type of object in .NET that surprisingly doesn't cause a universe-crushing paradox by simultaneously being 'nothing' and 'something' at the same time. Zero is our old, comforting friend: although just a number, one that signifies an absence of quantity.
My determination was that a 'Missing' object was the right choice for signifying to the Navigate method that I didn't want to argue unnecessarily:
public
object empty = System.Reflection.Missing.Value;
axWebBrowser1.Navigate("about:blank",ref empty, ref empty, ref empty,ref empty);
Coincidentally, I had come across a use for null just yesterday, when creating a sample project to demonstrate finding a memory leak using ANTS Profiler. Needless to say, the demonstration was a success, although not in the way that I had planned.
using
System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
namespace EventsExample
{
static class Program
{
static Form1 theForm;
[STAThread]
static void Main()
{
Control.CheckForIllegalCrossThreadCalls = false;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
theForm = new Form1();
Thread workThread = new Thread(new ThreadStart(DoWork));
workThread.Start();
Application.Run(theForm);
theForm.Dispose();
}
public static void DoWork()
{
CacheManager c = new CacheManager();
c.CacheClear += new CacheEventHandler(theForm.c_onCacheClear);
c.CheckCache();
}
}
The demonstration was meant to prove that if the DoWork method was running in a background thread, then it was holding a reference to an instance of Form1's c_onCacheClear method and therefore theForm would not be garbage collected after the window is closed, but I got more than I bargained for. Have you spotted it yet? Yes, the 'theForm 'object is declared as static and ANTS Profiler reports that the object is live after closing the window. Adding 'theForm=null;' after Application.Run(theForm); will get the garbage collector to dispose theForm.
Understanding the meanings behind .NET's three nothings is another way to help improve the code that you write.