Double question marks ('??') vs if when assigning same var

My advice would be to inspect the IL (intermediate language) and compare the different results. You can then see exactly what each boils down to and decide what is more optimized. But as Adam said in his comment, you're most likely best to focus on readability/maintainability over performance in something so small.

EDIT: you can view the IL by using the ILDASM.exe that ships with visual studio and open your compiled assembly.


Don't worry about the performance, it will be negligible.

If you are curious about it, write some code to test the performance using Stopwatch and see. I suspect you'll need to do a few million iterations to start seeing differences though.

You can also never assume about the implementation of things, they are liable to change in future - invalidating your assumptions.

My assumption is the performance difference is likely very, very small. I'd go for the null coalescing operator for readability personally, it is nice and condense and conveys the point well enough. I sometimes do it for lazy-load checking:

_lazyItem = _lazyItem ?? new LazyItem();

Although performance for ?? is negligible, the side effect sometimes may not be negligible. Consider the following program.

using System;
using System.Diagnostics;
using System.Threading;

namespace TestProject
{
    class Program
    {
        private string str = "xxxxxxxxxxxxxxx";
        public string Str
        {
            get
            {
                return str;
            }
            set
            {
                if (str != value)
                {
                    str = value;
                }
                // Do some work which take 1 second
                Thread.Sleep(1000);
            }
        }

        static void Main(string[] args)
        {
            var p = new Program();

            var iterations = 10;

            var sw = new Stopwatch();
            for (int i = 0; i < iterations; i++)
            {
                if (i == 1) sw.Start();
                if (p.Str == null)
                {
                    p.Str = "yyyy";
                }
            }
            sw.Stop();
            var first = sw.Elapsed;

            sw.Reset();
            for (int i = 0; i < iterations; i++)
            {
                if (i == 1) sw.Start();
                p.Str = p.Str ?? "yyyy";
            }
            sw.Stop();
            var second = sw.Elapsed;

            Console.WriteLine(first);
            Console.WriteLine(second);

            Console.Write("Ratio: ");
            Console.WriteLine(second.TotalMilliseconds / first.TotalMilliseconds);
            Console.ReadLine();
        }

    }
}

Run result on my PC.

00:00:00.0000015
00:00:08.9995480
Ratio: 5999698.66666667

Because there is an extra assignment using ??, and the performance of the assignment sometimes might not guaranteed. This might lead to a performance issue.

I would rather use if( null == A ) A = B; instead of A = A ?? B;.