After my article Top 15 Underutilized Features of .NET provoked an interesting discussion. I was curious to learn which method is faster– ?? (null coalescing operator), GetValueOrDefault method or ?: (conditional operator). Recently I read in Stack Overflow that most people believe that the GetValueOrDefault method is the fastest among these three. However, I decided to do my research. I am not trying to micro-optimize. I think it won’t matter in 99% of the cases which one of the three approaches you are going to use. Usually, you should choose the one that it is easier to maintain. I am not going to argue which one is more readable because that is another topic. Rather I am going to present to you my research’s benchmark results.

Null Coalescing Operator ??

The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand. A nullable type can contain a value, or it can be undefined. The ?? operator defines the default value to be returned when a nullable type is assigned to a non-nullable type.

GetValueOrDefault Method

Retrieves the value of the current Nullable<T> object, or the object’s default value. It is faster than ?? operator.

If you don’t specify a default value as a parameter to the method, the default value of the used type is going to be used.

Conditional Operator ?:

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.

condition ? first_expression : second_expression;

The condition must evaluate to true or false. If the condition is true, first_expression is evaluated and becomes the result. If the condition is false, second_expression is evaluated and becomes the result. Only one of the two expressions is evaluated.

GetValueOrDefault and Null Coalescing Operator Internals

You can find the source code for the GetValueOrDefault method on the following URL. There are two overloads for the method, one without parameters and one that requires the default value to be returned if the variable is null.

As the code tells, under the hood the GetValueOrDefault method uses the conditional operator.

All of this was not enough for me so I decompiled the following code to find out how it is translated into Common Intermediate Language (CIL). For the job, I used the free .NET decompiler- JustDecompile.

GetValueOrDefault CIL

Null Coalescing Operator CIL

As far as I can cope the CIL code I think that the x ?? y is transformed into x.HasValue ? x.GetValueOrDefault() : y. Which automatically should mean that most probably the former is going to be much faster than the later.

Which Works Faster- Null Coalescing Operator or GetValueOrDefault or Conditional Operator

To benchmark the different test cases I created a specialized profiler class.

All gauges are performed in Release configuration. The correct tool to use when writing benchmarks is Stopwatch in the System.Diagnostics namespace. (I note that this namespace is well-named; everything in here is useful for diagnosing problems). DateTime.Now is the wrong tool for the job, it was designed to solve a different problem. It is harder to use than Stopwatch and has thousands or millions of times less precision. Avoid it entirely when writing benchmarks in C#.

Failing to take GC costs into account can cause you to fail to measure the true cost of an operation, or cause you to charge that cost to the wrong code. What I usually do when benchmarking, particularly if it is comparative benchmarking, is to force the garbage collector to do a full collection of all three generations before and after every test.

To force the garbage collector to do a complete collection use the following code:

These are the six test cases that I benchmarked.

Performed Test Cases

(1.) GetValueOrDefault with default value set

(2.) Null Coalescing Operator with default value set

(3.) Conditional Operator with default value set

(4.) GetValueOrDefault without default value

(5.) Null Coalescing Operator returning default value 0

(6.) Conditional Operator returning default value 0

Home-made Benchmark Results

After several test runs, you can view the results from my research.



Also, I have created two comparison charts for better visualization of the results. Keep in mind that I excluded the results for third test case because I don’t know why but this case was much slower than the rest.

Find below the chart containing the average execution times for all test cases.



Find below the chart containing the average ticks for all test cases.



As you can see from my results when you want to return a default value different than the default for the current Nullable type, the best performer is the null coalescing operator (??). However, when you want to return the default value for the type GetValueOrDefault method is a little bit faster.

Pro Benchmark through JustTrace

The results from my home-made benchmark were not enough for me so I installed JustTrace (2-in-1 memory and performance profiler for .NET and native apps). The results for the same test cases were slightly different.



For returning a different default value, the GetOrDefaultValue method was over 8% faster than the null coalescing operator. Also, its was again a little bit faster among the test cases where the default value for the nullable type is returned.