Home » Advanced CSharp » Making values nullable

Making values nullable

A04C0009 First, a little history lesson.

When .NET was first released, we had value types and object types.  The difference between the two is that you do not have to set aside memory for a value type because the memory is placed on the stack.  So ints, doubles, and structures are all value types.

However, even though the value type is treated differently, it is still ultimately inherited from the Object type.  This is good because it allows us to create a method

public void Foo(object o)
{
}

that an integer can be passed to

Foo(1);

But, what happens when we have a method we want to pass an integer to but if a value has never been assigned to it, we just want to ignore it?

In the old days, we’d have to write our function like this:

public void Foo(object o)
{

    if (o != null)
    {
        int realValue = (int)o;
        // do something real here.
    }
}

Of course, we’d need to add some extra code to verify that o really was an integer before we attempted to cast it to an int.  Not exactly “type safe.”

So an extension to the language was added that allows us to make values nullable.  By simply placing a question mark after the type declaration, we can assign a null value and our method allows us to have both a strongly typed parameter and the ability to make it null.

public void Foo(int? o)
{
    if (o != null)
    {
        // do something with o
    }
}

and we can call it like this:

int? i = 1;
Foo(i);

or if we don’t have a variable we are using, we can call it directly:

Foo(1);

Finally, if you have a variable of type int? that you need to pass to a parameter of type int, you’ll need to do some casting:

Here’s the method that is expecting an int:

public void Foo(int o)
{
}

and here is how you would cast it if the variable is an int?

int? i = 1;
Foo((int)i);

If you are working the other way around, you do not need to provide any casting operators.

 

Other post in Advanced CSharp

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • Dispose, Finalize and SuppressFinalizeDispose, Finalize and SuppressFinalize I got the following question recently. What is the difference between Dispose and SupressFinalize in garbage collection?” The problem with this question is it assumes Dispose and […]
  • Delegates in .NETDelegates in .NET I received the following question:What is a delegate?  What problem does it solve?  and When might I use a delegate?A delegate is essentially a function pointer.  We have used […]
  • Stackalloc in CSharpStackalloc in CSharp In the last few weeks we've looked at several keywords from the CSharp language that allow us to deal with memory management directly.  Stackalloc is another keyword from that […]
  • CSharp fixed keywordCSharp fixed keyword Since I've already mentioned my bias against using unsafe mode in this post:Advanced CSharp - unsafe modeI'll skip my normal rant about that.  Just suffice it to say they don't […]
  • Using Sealed in CSharpUsing Sealed in CSharp Another potentially useful but not very popular keyword in CSharp is the sealed keyword.  While not a keyword that you will need to use often, it is a critical keyword for framework […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.