Realizing Results

my personal notes on C#, SharePoint and object oriented programming

  Home  |   Contact  |   Syndication    |   Login
  7 Posts | 0 Stories | 2 Comments | 0 Trackbacks

News

Archives

About Me

Tuesday, August 31, 2010 #

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 


below is the code snippet on how to update a field in sharepoint list which contains multiple chioce value

using(SPWeb oWebsite = SPContext.Current.Site.AllWebs["Site_Name"])
{
  SPList oList = oWebsite.Lists["listname"];
  SPListItem oListItem = oList.Items[0];

  oListItem["columnname"] = "choice1;#chioce2;#chioce3"
  oListItem.Update();
}