Geeks With Blogs
Realizing Results my personal notes on C#, SharePoint and object oriented programming

Many times we see any of these two different convention used by different programmers for null checks. Even have seen many seasoned java professional who used the first form and argued that X!=null may hit the performance so one should use null!=X, but if you go through the c# language specification there is no such difference as such in both the forms and  are translated to x.HasValue during compilation. below is section as explained in CLS

From C# language Specification:

Section 14.9.9 Equality operators and null
The == and != operators permit one operand to be a value of a nullable type and the other to have the null type (§11.2.7), even if no predefined or user-defined operator (in unlifted or lifted form) exists for the operation.

For an operation of one of the forms
x == null null == x x != null null != x where x is an expression of a nullable type,

if operator overload resolution (§14.2.4) fails to find an applicable operator, the result is instead computed from the HasValue property of x. Specifically, the first two forms are translated into !x.HasValue, and last two forms are translated into x.HasValue.

but still i was not so convinced with the fact and wanted to test actual execution time for these two. the below is the sample program for the test i used

 class TestClass
    {
        public static void Main()
        {
            TestClass x1 = new TestClass();
            for (int i = 0; i < 10; i++)
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                Console.WriteLine("{0}, {1}, {2}", "x1 != null", x1 != null, sw.ElapsedTicks);
                sw.Restart();
                Console.WriteLine("{0}, {1}, {2}", "null != x1", null != x1, sw.ElapsedTicks);
                sw.Restart();
                Console.WriteLine("{0}, {1}, {2}", "x1 == null", x1 == null, sw.ElapsedTicks);
                sw.Restart();
                Console.WriteLine("{0}, {1}, {2}", "null == x1", null == x1, sw.ElapsedTicks);
                sw.Stop();
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }

and the output of the program is below except for the first iteration

x1 != null, True, 906
null != x1, True, 787
x1 == null, False, 860
null == x1, False, 765

x1 != null, True, 869
null != x1, True, 783
x1 == null, False, 869
null == x1, False, 836

x1 != null, True, 841
null != x1, True, 824
x1 == null, False, 899
null == x1, False, 806

x1 != null, True, 845
null != x1, True, 832
x1 == null, False, 912
null == x1, False, 805

x1 != null, True, 869
null != x1, True, 845
x1 == null, False, 916
null == x1, False, 832

output shows that there is marginal difference in x1 != null and null != x1 but still null != x1 behaves better than the other counterpart 

Posted on Tuesday, August 31, 2010 4:51 PM | Back to top


Comments on this post: Difference between comparisons null!= x and x!=null

# re: Difference between comparisons null!= x and x!=null
Requesting Gravatar...
10 iterations is barely a benchmark. The IL is identical thus any difference in performance is simply randomness. I think x1 != null makes more sense because it reads like English. NOUN VERB ADJECTIVE. null != x1 is inverted ADJECTIVE VERB NOUN. It's like "Tree is tall?" vs "Tall is tree?".
Left by Ryan on Sep 03, 2010 10:15 PM

Your comment:
 (will show your gravatar)


Copyright © Manish Sati | Powered by: GeeksWithBlogs.net | Join free