Ken Brubaker

The ClavèCoder

<December 2008>
SuMoTuWeThFrSa
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910


Navigation

Subscriptions

News

Kenneth Brubaker
Senior Application Architect

Locations of visitors to this page

Post Categories



Nullable performance usage guideline

The BCL team published an analysis of the fastest way to get a value out of a nullable type. The preferred method is:

int? nullableInt = GetMyNullableValue();
int value;
if (nullableInt.HasValue)
{
    value = nullableInt.GetValueOrDefault();
}

The common method is:

int? nullableInt = GetMyNullableValue();
int value;
if (nullableInt.HasValue)
{
    value = nullableInt.Value;
}

In their analysis using the Value property in the scenario above is 16% slower than using the GetValueOrDefault method, because Value also calls HasValue, so it gets called twice.

My take? I thought you properties for fast access. Why is the method faster than the property? It doesn't make a lot of sense to me. Like StringBuilder vs. String, I would recommend using Value except for tight loops where the performance matters. Otherwise, people will be wondering what the default value has to do with anything. Answer? null (or Nothing in Visual Basic) :-)

posted on Friday, July 01, 2005 3:45 AM by kenbrubaker





Powered by Dot Net Junkies, by Telligent Systems