Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 6th, 2012  |  syntax: None  |  size: 3.78 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Is the null coalesce operator thread safe?
  2. public class Foo
  3.     {
  4.         Object _bar;
  5.         public Object Bar
  6.         {
  7.             get { return _bar ?? new Object(); }
  8.             set { _bar = value; }
  9.         }
  10.     }
  11.        
  12. get { return _bar != null ? _bar : new Object(); }
  13.        
  14. public static T GetValue<T>(ref T value) where T : class, new()
  15.         {
  16.             return value ?? new T();
  17.         }
  18.        
  19. public static T GetValue<T>(ref T value) where T : class, new()
  20.         {
  21.             T result = value;
  22.             if (result != null)
  23.                 return result;
  24.             else
  25.                 return new T();
  26.         }
  27.        
  28. .method public hidebysig specialname instance object get_Bar() cil managed
  29. {
  30.     .maxstack 2
  31.     .locals init (
  32.         [0] object CS$1$0000)
  33.     L_0000: nop
  34.     L_0001: ldarg.0
  35.     L_0002: ldfld object ConsoleApplication1.Program/MainClass::_bar
  36.     L_0007: dup
  37.     L_0008: brtrue.s L_0010
  38.     L_000a: pop
  39.     L_000b: newobj instance void [mscorlib]System.Object::.ctor()
  40.     L_0010: stloc.0
  41.     L_0011: br.s L_0013
  42.     L_0013: ldloc.0
  43.     L_0014: ret
  44. }
  45.        
  46. get { return _bar != null ? _bar : new Object(); }
  47.        
  48. get
  49. {
  50.     Object result = _bar;
  51.     if(result == null)
  52.     {
  53.         result = new Object();
  54.     }
  55.     return result;
  56. }
  57.        
  58. public class Foo
  59. {
  60.     Object _bar = new Object();
  61.     public Object Bar
  62.     {
  63.         get { return _bar; }
  64.         set { _bar = value ?? new Object(); }
  65.     }
  66. }
  67.        
  68. .method public hidebysig specialname instance object get_Bar_NullCoalesce() cil managed
  69. {
  70.     .maxstack 8
  71.     L_0000: ldarg.0                         // Load argument 0 onto the stack (I don't know what argument 0 is, I don't understand this statement.)
  72.     L_0001: ldfld object CoalesceTest::_bar // Loads the reference to _bar onto the stack.
  73.     L_0006: dup                             // duplicate the value on the stack.
  74.     L_0007: brtrue.s L_000f                 // Jump to L_000f if the value on the stack is non-zero.
  75.                                             // I believe this consumes the value on the top of the stack, leaving the original result of ldfld as the only thing on the stack.
  76.     L_0009: pop                             // remove the result of ldfld from the stack.
  77.     L_000a: newobj instance void [mscorlib]System.Object::.ctor()
  78.                                             // create a new object, put a reference to it on the stack.
  79.     L_000f: ret                             // return whatever's on the top of the stack.
  80. }
  81.        
  82. .method public hidebysig specialname instance object get_Bar_IntermediateResultVar() cil managed
  83. {
  84.     .maxstack 1
  85.     .locals init (
  86.         [0] object result)
  87.     L_0000: ldarg.0
  88.     L_0001: ldfld object CoalesceTest::_bar
  89.     L_0006: stloc.0
  90.     L_0007: ldloc.0
  91.     L_0008: brtrue.s L_0010
  92.     L_000a: newobj instance void [mscorlib]System.Object::.ctor()
  93.     L_000f: stloc.0
  94.     L_0010: ldloc.0
  95.     L_0011: ret
  96. }
  97.  
  98. .method public hidebysig specialname instance object get_Bar_TrinaryOperator() cil managed
  99. {
  100.     .maxstack 8
  101.     L_0000: ldarg.0
  102.     L_0001: ldfld object CoalesceTest::_bar
  103.     L_0006: brtrue.s L_000e
  104.     L_0008: newobj instance void [mscorlib]System.Object::.ctor()
  105.     L_000d: ret
  106.     L_000e: ldarg.0
  107.     L_000f: ldfld object CoalesceTest::_bar
  108.     L_0014: ret
  109. }
  110.        
  111. public object Bar_NullCoalesce
  112. {
  113.     get { return this._bar ?? new Object(); }
  114. }
  115.  
  116. public object Bar_IntermediateResultVar
  117. {
  118.     get
  119.     {
  120.         object result = this._bar;
  121.         if (result == null) { result = new Object(); }
  122.         return result;
  123.     }
  124. }
  125.  
  126. public object Bar_TrinaryOperator
  127. {
  128.     get { return this._bar != null ? this._bar : new Object(); }
  129. }
  130.        
  131. List<int> l = null;
  132. var x = l ?? new List<int>();
  133.        
  134. [STAThread]
  135. public static void Main(string[] args)
  136. {
  137.     List<int> list = null;
  138.     if (list == null)
  139.     {
  140.         new List<int>();
  141.     }
  142. }