c# - DateTime.Now inside object initialiser -
if do:
var foo = new foo {     created = datetime.now,     modified = datetime.now }; is there chance compiler optimisation going on ensures 2 date properties equal, or must read now temporary variable first myself?
the compiler not supposed omit datetime.now, regardless of optimization. ensure fields initialized same value, consider this:
struct foo {     public datetime created;     public datetime modified;     public foo(datetime dt)     {         created = modified = dt;     } }  var foo = new foo(datetime.now); the probability created != modified, if initialized separately, rather high. following loops on system (in debug , release) never makes more few sousand iterations.
for (int = 0; < 1000000000; i++) {     var foo = new foo     {         created = datetime.now,         modified = datetime.now     };     if (foo.created != foo.modified)     {         console.writeline("{0} {1} {2}", foo.created.ticks, foo.modified.ticks, i);         break;     } }