Skip to: Site menu | Main content

Welcome there !

Emmanuel Desigaud's blog. Personnal, professional. Fun pictures, kewl web links, usefull software ....
« Moved from bloglines to Google Reader :: Book Review : Pro CSS and HTML Design Patterns »
2008-05-09

String conditional operator & MSIL

Categories: Software Language : English (EU)

As good geek discussion, with some colleagues we were wondering how was implemented the string conditional operator in C#.

Here's a quick comparaison between a "classical" if-then-else and a string ? condition. The analyze focus on the MSIL code generated.

Basic C# condition :

String foo = "myString";
String aa;
  
if (foo.Length > 0)
    aa = foo;
else
    aa = String.Empty;

and the MSIL code :

   1:  .method private hidebysig static void  Main(string[] args) cil managed
   2:  {
   3:    .entrypoint
   4:    // Code size       34 (0x22)
   5:    .maxstack  2
   6:    .locals init ([0] string foo,
   7:             [1] string aa,
   8:             [2] bool CS$4$0000)
   9:    IL_0000:  nop
  10:    IL_0001:  ldstr      "myString"
  11:    IL_0006:  stloc.0
  12:    IL_0007:  ldloc.0
  13:    IL_0008:  callvirt   instance int32 [mscorlib]System.String::get_Length()
  14:    IL_000d:  ldc.i4.0
  15:    IL_000e:  cgt
  16:    IL_0010:  ldc.i4.0
  17:    IL_0011:  ceq
  18:    IL_0013:  stloc.2
  19:    IL_0014:  ldloc.2
  20:    IL_0015:  brtrue.s   IL_001b
  21:    IL_0017:  ldloc.0
  22:    IL_0018:  stloc.1
  23:    IL_0019:  br.s       IL_0021
  24:    IL_001b:  ldsfld     string [mscorlib]System.String::Empty
  25:    IL_0020:  stloc.1
  26:    IL_0021:  ret
  27:  } // end of method Program::Main

String conditional operator :

String foo = "myString";
String aa = foo.Length > 0 ? foo : String.Empty;

MSIL code :

   1:  .method private hidebysig static void  Main(string[] args) cil managed
   2:  {
   3:    .entrypoint
   4:    // Code size       26 (0x1a)
   5:    .maxstack  2
   6:    .locals init ([0] string foo,
   7:             [1] string aa)
   8:    IL_0000:  nop
   9:    IL_0001:  ldstr      "myString"
  10:    IL_0006:  stloc.0
  11:    IL_0007:  ldloc.0
  12:    IL_0008:  callvirt   instance int32 [mscorlib]System.String::get_Length()
  13:    IL_000d:  ldc.i4.0
  14:    IL_000e:  bgt.s      IL_0017
  15:    IL_0010:  ldsfld     string [mscorlib]System.String::Empty
  16:    IL_0015:  br.s       IL_0018
  17:    IL_0017:  ldloc.0
  18:    IL_0018:  stloc.1
  19:    IL_0019:  ret
  20:  } // end of method Program::Main

What can we conclude ? The conditional operator is not that bad and may help you to optimize a little bit more your code. One argument in favor of the classical if-then-else is that it may be more readable (I don't necesssary agree) but on the other side, does slightly more operations and instantiate one more variable in the stack.

To finish, I think that no-one will notice a difference of performance between the two methods so use the one you feel the more confortable.

PermalinkPermalink