Thursday, March 19, 2015

C#: Default value for int? and other nullable types

If you’re a C# developer, you’re probably aware of the nullable versions of primitive types such as int. Just declare the variable with a question mark added to the type, like so:

int? i;

And presto, you have an int variable to which you can assign null!  Very handy for working with nullable number fields in databases, among other uses.

Before today, the above was pretty much the extent of my knowledge of C# nullable types.  As far as I was concerned, they were just “magic” versions of the corresponding primitives.

I ran into a situation today, though, that led me to investigate more deeply.  I had a class with a nullable type field for which no default value was explicitly assigned, as in this simplified example:

    Class MyClass
         public int? I { get; set; }

I needed to determine whether the field had been assigned a value other than the default value.  That led me to ask the question: What is the default value for an unassigned nullable int? instance variable in C#?  Is it null, like for reference types?  Or is it 0, like for int?

Surprisingly, searching Google didn’t immediately lead to a clear answer; nor did there seem to be a question on StackOverflow asking for this particular information.  The MSDN article Nullable Types (which was the first Google hit for terms C# nullable default value) didn’t have a clear answer.

It was the work of just a couple of minutes to write and run a bit of code to test the behavior using LINQPad.  The answer: The default value of a C# int? is null.

So int? defaults to null.  But why?

I was curious: Why is the default value null, not 0?  So I did some investigating, and ended up learning the following:

  • int? is syntactic sugar for the type Nullable<T> (where T is int), a struct.  (Per that Nullable Types MSDN article.)
  • The Nullable<T> type has a bool HasValue member, which when false, makes the Nullable<T> instance "act like" a null value.  In particular, the Nullable<T>.Equals method  is overridden to return true when a Nullable<T> with HasValue==false is compared with an actual null value.
  • From the C# Language Specification 11.3.4, a struct instance's initial default value is all of that struct's value type fields set to their default value, and all of that struct's reference type fields set to null. 
  • The default value of a bool variable in C# is false (reference). 

Therefore, the HasValue property of a default Nullable<T> instance is false; which in turn makes that Nullable<T> instance itself act like null.

Since there was no readily-available information on this topic in StackOverflow, I went ahead and posted it there as a new question and answer.

A null int? just simulates an actual null value

An interesting aspect of an int? merely “simulating” a null value, rather than actually being one, is that you can call methods on a null-value int? without a NullReferenceException occurring!  For example, this code using a null String throws a NullReferenceException:

    String s = null;
    s.GetHashCode(); // Throws NullReferenceException

The same code, though, used with a null Nullable<T>, does not throw an exception:

    int? n = null;
    n.GetHashCode(); // Returns 0

Friday, December 05, 2014

Quick email tips!

Here are a couple brief email tips that are top of mind:

Set Recipients Last!  When writing a long email message, leave the To (Recipients) field BLANK until last, so that if you inadvertently hit Send before the message is actually finished and ready to go, it won't go anywhere!

Attach Attachments First!  As soon as you type the words “see attachment” or “please find attached” into the message body, stop typing, and add the actual attachment to the email!  If you assume you’ll remember to add the attachment after you’ve finished typing up the message body, sometimes you won’t remember!

Wednesday, October 15, 2014

System.Uri (.NET Framework 4.5) – A Visual Guide

Since I’ve repeated enough times the somewhat cumbersome exercise of inferring what the various properties of the .NET Framework (as of version 4.5) System.Uri class return for a “typical” HTTP/HTTPS URL from the official documentation – and generally resorted to manual testing to be 100% certain of the behavior – here’s a visual guide.

Given a System.Uri instance constructed as follows (C#):

System.Uri uri = new System.Uri("");

Here’s what the various relevant properties of that Uri instance return:

Property String value

The Segments property returns the following String array:

String[] {

Hopefully this will save a little time for all of you as well as for me!

Wednesday, September 10, 2014

C# – Shorten null check around foreach loops

This is another blog post filed under “So I can remember this for next time. If it helps you too, great!”

In C#, when looping over a collection where the collection object itself might be null, if the desired behavior is to just loop zero times if the collection object is null, typical code would be:

List<MyType> list = PopulateList(); // Some method that returns a List<MyType>, or null 
if (list != null) 
  foreach (MyType mt in list) 
    // Do stuff with mt... 

Using the C# null-coalescing operator ??, the above code can be condensed to:

List<MyType> list = PopulateList(); // Some method that returns a List<MyType>, or null
foreach (MyType mt in list ?? Enumerable.Empty<MyType>) 
  // Do stuff with mt... 

In that second example, if list is non-null, then the foreach iterates over it as normal. If list is null, then the foreach iterates over an empty collection -- so zero iterations are performed.

Wednesday, August 06, 2014

Double-click-drag to select entire words

Here’s a quick tip on selecting text with the mouse that I’m blogging as much to help myself remember as to inform you, the reader:

You can double-click-drag – that is, double-click, and continue holding the mouse button on the 2nd click – to select multiple entire words from a block of text.  This obviates the need to position the mouse cursor exactly over the small space between words when selecting entire words or sentences, both when beginning and ending the selection.

Bonus tip: You can triple-click to select an entire line or paragraph of text.  (This one I do remember and use frequently – and get annoyed by those few applications that don’t support it.)

These tips work in most applications on both Windows and Mac.