tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

Nullable types – what’s faster?

20 Oct 2008 .NET

Today I was working with nullable types – short? for my instance. I was wondering what’s the fastest way of getting value and also testing whether the value is there.

HasValue vs. != null

Well test on my notebook on .NET FW 3.5 in VPC, optimization turned on shows that HasValue is faster. Roughly twice on my configuration. Also the IL code looks different.

Using Foo.Value vs. (short)Foo

Here I didn’t measured any difference. And the code is pretty much same. It’s not exhaustive testing (and you know – I believe only in stats I adulterated 😉).

I did about 10 runs for each test and looked to IL (it’s fastest way to prove your idea is wrong or not).

What about you? Similar results?

Profile Picture Jiří Činčura is an independent developer focusing on data and business layers, language constructs, parallelism and databases. Specifically Entity Framework, asynchronous and parallel programming, cloud and Azure. He's Microsoft Most Valuable Professional and you can read his articles, guides, tips and tricks at