Daily Knowledge Drop
Value Types
by default, have to have a value and cannot be null in C# - even if uninitialized. This can lead to unexpected results if comparing a value type to null instead of the default value.
Null comparison
Consider the below example:
class Program
{
static Point pointValue;
static int IntValue;
static void Main(string[] args)
{
// comparing an uninitialized Point to null
Console.WriteLine(pointValue == null);
Console.WriteLine(IntValue == null);
}
}
The output for this is:
False
False
Both the Point and int types are value types
and as such have default values of (0, 0) and 0 respectively (not null), even if uninitialized.
So instead of comparing to null, we need to check if they are empty
or have the default value
for the type.
IsEmpty comparison
Some value types (not but all), have a IsEmpty
property which will return true if uninitialized or explicitly set to the default value:
class Program
{
static Point pointValue;
static Point pointValue2 = new Point(0,0);
static int IntValue;
static void Main(string[] args)
{
Console.WriteLine(pointValue.IsEmpty);
Console.WriteLine(pointValue2.IsEmpty);
}
}
The output for this is now:
True
True
Both variables are considered empty as they both have the default value. int
does not have an IsEmpty property, so cannot be checked using this method.
default comparison
The default
keyword (which will produced the default value of a type) can be compared to a value type to determine if it has the default value:
class Program
{
static Point pointValue;
static Point pointValue2 = new Point(0,0);
static int IntValue;
static void Main(string[] args)
{
Console.WriteLine(pointValue == default);
Console.WriteLine(pointValue2 == default);
Console.WriteLine(IntValue == default);
}
}
The output for this is now:
True
True
True
As you can see, default
works for Point and int, and in fact it will work with all types.
Nullable types
A value type can made nullable
indicating it can have a null value. This is done using the ?
indicator. In the below sample, each variable is now marked with ?
:
class Program
{
static Point? pointValue;
static Point? pointValue2 = new Point(0,0);
static int? IntValue;
static void Main(string[] args)
{
Console.WriteLine(pointValue == null);
Console.WriteLine(pointValue2 == null);
Console.WriteLine(IntValue == null);
}
}
Now when comparing these values to null
the output is as follows:
True
False
True
If a nullable value type is not initialized, it will have a null value.
Notes
Value type comparison
is a small detail in a large application, but if done incorrectly can lead to unexpected results. Its important to understand how the value type variables are set (or not set) and how each one can be checked for a null or default value.
References
Common C# Programming Mistake #2: Misunderstanding default values for uninitialized variables
Daily Drop 80: 24-05-2022
At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.
The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.