tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

Nullable types – what’s faster?

20 Oct 2008 1 mins .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 .NET, C# and Firebird expert. He focuses on data and business layers, language constructs, parallelism, databases and performance. For almost two decades he contributes to open-source, i.e. FirebirdClient. He works as a senior software engineer for Microsoft. Frequent speaker and blogger at