Guest User

Untitled

a guest
Aug 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. Constructor based object initialization
  2. Employee currentUser = new Employee();
  3. currentUser.Name = "Bob";
  4.  
  5. Employee currentUser = new Employee() { Name = "Bob" };
  6.  
  7. internal class Foo
  8. {
  9. public string Bar { get; set; }
  10. }
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. Foo f = new Foo()
  16. {
  17. Bar = "Baaz"
  18. };
  19. Console.WriteLine("Now the old way sugar");
  20. Foo f2 = new Foo();
  21. f2.Bar = "Baaz";
  22.  
  23. }
  24. }
  25.  
  26. IL_0000: nop
  27. IL_0001: newobj instance void SimpleDataPlayGround.Foo::.ctor()
  28. IL_0006: stloc.2
  29. IL_0007: ldloc.2
  30. IL_0008: ldstr "Baaz"
  31. IL_000d: callvirt instance void SimpleDataPlayGround.Foo::set_Bar(string)
  32. IL_0012: nop
  33. IL_0013: ldloc.2
  34. IL_0014: stloc.0
  35. IL_0015: ldstr "Now the old way sugar"
  36. IL_001a: call void [mscorlib]System.Console::WriteLine(string)
  37. IL_001f: nop
  38. IL_0020: newobj instance void SimpleDataPlayGround.Foo::.ctor()
  39. IL_0025: stloc.1
  40. IL_0026: ldloc.1
  41. IL_0027: ldstr "Baaz"
  42. IL_002c: callvirt instance void SimpleDataPlayGround.Foo::set_Bar(string)
  43. IL_0031: nop
  44. IL_0032: ret
  45.  
  46. Employee tmp = new Employee();
  47. tmp.Name = "Bob";
  48. Employee currentUser = tmp;
  49.  
  50. Employee currentUser = new Employee { Name = "Bob" };
  51.  
  52. currentUser = new Employee { Name = currentUser.Name + "Foo" };
Add Comment
Please, Sign In to add comment