Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. TestRef t = new TestRef();
  6. t.Something = "Foo";
  7.  
  8. DoSomething(t);
  9. Console.WriteLine(t.Something);
  10. }
  11.  
  12. static public void DoSomething(TestRef t)
  13. {
  14. t.Something = "Bar";
  15. }
  16. }
  17.  
  18.  
  19. public class TestRef
  20. {
  21. public string Something { get; set; }
  22. }
  23.  
  24. TestRef t = new TestRef();
  25. t.Something = "Foo";
  26. DoSomething(ref t);
  27.  
  28. void DoSomething(ref TestRef t)
  29. {
  30. t = new TestRef();
  31. t.Something = "Not just a changed t, but a completely different TestRef object";
  32. }
  33.  
  34. int x = 1;
  35. Change(ref x);
  36. Debug.Assert(x == 5);
  37. WillNotChange(x);
  38. Debug.Assert(x == 5); // Note: x doesn't become 10
  39.  
  40. void Change(ref int x)
  41. {
  42. x = 5;
  43. }
  44.  
  45. void WillNotChange(int x)
  46. {
  47. x = 10;
  48. }
  49.  
  50. public void Method1(object obj) {
  51. obj = new Object();
  52. }
  53.  
  54. public void Method2(object obj) {
  55. obj = _privateObject;
  56. }
  57.  
  58. using System;
  59.  
  60. class Program
  61. {
  62. static void Main(string[] args)
  63. {
  64. TestRef t = new TestRef();
  65. t.Something = "Foo";
  66.  
  67. DoSomething(t);
  68. Console.WriteLine(t.Something);
  69.  
  70. }
  71.  
  72. static public void DoSomething(TestRef t)
  73. {
  74. t = new TestRef();
  75. t.Something = "Bar";
  76. }
  77. }
  78.  
  79.  
  80.  
  81. public class TestRef
  82. {
  83. private string s;
  84. public string Something
  85. {
  86. get {return s;}
  87. set { s = value; }
  88. }
  89. }
  90.  
  91. static public void DoSomething(ref TestRef t)
  92. {
  93. t = new TestRef();
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement