Guest User

Untitled

a guest
Jan 19th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. using System;
  2. class Program
  3. {
  4. static void Main(string[] args)
  5. {
  6. int x = 2;
  7. int y = 20;
  8. Console.WriteLine(Add(x, y));
  9. }
  10.  
  11. static int Add(int x, int y)
  12. {
  13. int ans = x + y;
  14. x = 20;
  15. y = 40;
  16. // return x+y;
  17. return ans;
  18. //returns 22
  19. }
  20. }
  21.  
  22. using System;
  23. class Program
  24. {
  25. static void Main(string[] args)
  26. {
  27. int x = 2;
  28. int y = 20;
  29. Console.WriteLine(Add(x, y));
  30. }
  31.  
  32. static int Add(int x, int y)
  33. {
  34. int ans = x + y;
  35. x = 20;
  36. y = 40;
  37. return x+y;
  38. // return ans;
  39. //returns 60
  40. }
  41. }
  42.  
  43. int ans = x + y;
  44.  
  45. static int Add(int x, int y)
  46. {
  47. int ans = x + y; // evaluates ans = x + y = 2 + 20 = 22
  48. x = 20; // here ans = 22, x = 20, y = 20
  49. y = 40; // here ans = 22, x = 20, y = 40
  50. return ans; // returns ans which is still 22 since x & y are independent
  51. }
  52.  
  53. return x + y;
  54.  
  55. static int Add(int x, int y)
  56. {
  57. int ans = x + y; // ans = 22, x = 2, y = 20
  58. x = 20; // ans = 22, x = 20, y = 20
  59. y = 40; // ans = 22, x = 20, y = 40
  60. return x+y; // evaluates x + y = 60
  61. }
  62.  
  63. ans = x + y;
  64.  
  65. // parameters in: x = 2, y = 20.
  66. int ans = x + y;
  67. // now ans contains the value 22.
  68. x = 20;
  69. y = 40;
  70. // now x and y has changed, but the value in ans
  71. // is already calculated and doesn't change.
  72. return ans;
  73. // returns 22;
  74.  
  75. // parameters in: x = 2, y = 20.
  76. int ans = x + y;
  77. // now ans contains the value 22.
  78. x = 20;
  79. y = 40;
  80. // now x and y has changed, but not ans.
  81. return x+y;
  82. // the value of the expression is calculated with the current
  83. // values, and 60 is returned.
  84. // the variable ans still contains 22, but that isn't used here.
  85.  
  86. using System;
  87. class Program
  88. {
  89. static void Main(string[] args)
  90. {
  91. int x = 2;
  92. int y = 20;
  93. Console.WriteLine(Add(x, y));
  94. // x is still 2, y is still 20
  95. }
  96.  
  97. static int Add(int x, int y)
  98. {
  99. int ans = x + y;
  100. // You calculate the parameters and store it in the local variable
  101. x = 20;
  102. y = 40;
  103. // You've adapted your local COPIES of the variables
  104. return ans;
  105. // You return the answer which was calculated earlier
  106. }
  107. }
  108.  
  109. using System;
  110. class Program
  111. {
  112. private class Numbers
  113. {
  114. public int X;
  115. public int Y;
  116. }
  117.  
  118. static void Main(string[] args)
  119. {
  120. Numbers num = new Numbers();
  121. num.x = 2;
  122. num.y = 20;
  123. Console.WriteLine(Add(num)); // Prints 2 + 20 = 22
  124. // num.x is now 20, and num.y is now 40
  125. Console.WriteLine(Add(num)); // Prints 20 + 40 = 60
  126. }
  127.  
  128. static int Add(Numbers num)
  129. {
  130. int ans = num.x + num.y;
  131. // You calculate the result from the public variables of the class
  132. num.x = 20;
  133. num.y = 40;
  134. // You change the values of the class
  135. return ans;
  136. // You return the answer which was calculated earlier
  137. }
  138. }
  139.  
  140. static void Main()
  141. {
  142. int x = 5; // Value type
  143. List<int> list = new List<int>(new [] { 1, 2, 3 }); // Reference type
  144.  
  145. ValueByValue(x); // x is still 5
  146. ReferenceByValue(list) // list still contains { 1, 2, 3 }
  147. ValueByReference(ref x); // x is now 10
  148. ReferenceByReference(ref list); // list is now a new list containing only { 4, 5, 6 }
  149. }
  150.  
  151. static void ValueByValue(int x)
  152. {
  153. x = 10; // Changes local COPY of x
  154. }
  155.  
  156. static void ReferenceByValue(List<int> list)
  157. {
  158. list = new List<int>(new [] { 4, 5, 6 }); // Changes local COPY of list
  159. }
  160.  
  161. static void ValueByReference(ref int x)
  162. {
  163. x = 10; // Changes the actual x variable in the Main method
  164. }
  165.  
  166. static void ReferenceByReference(ref List<int> list)
  167. {
  168. list = new List<int>(new [] { 4, 5, 6 }); // Changes the actual list in the Main method
  169. }
  170.  
  171. int x=50,y=20;
  172.  
  173. Example
  174. x+y=500;//INVALID
  175.  
  176. Example
  177. x=500;//VALID
  178. y=545*33+4;//VALID
  179.  
  180. static int Add(int x, int y)
  181. {
  182. int ans = x + y;
  183. x = 20;
  184. y = 40;
  185. // return x+y;
  186. return ans;
  187. //returns 22
  188. }
  189.  
  190. static int Add(int x, int y)
  191. {
  192. int ans = x + y;
  193. x = 20;
  194. y = 40;
  195. return x+y;
  196. // return ans;
  197. //returns 60
  198. }
  199.  
  200. static int Add(ref int x, int y) // x is the referenced variable in this example
  201.  
  202. using System;
  203. class Program
  204. {
  205. static void Main(string[] args)
  206. {
  207. int x = 2;
  208. int y = 20;
  209.  
  210.  
  211. Console.WriteLine("SUM :: " + AddByValue(x, y)); // Call by value
  212. Console.WriteLine("X :: " + x + ", Y :: " + y); // Nothing change to variable
  213.  
  214. Console.WriteLine("SUM :: " + AddByRef(ref x, ref y)); // Call by reference
  215. Console.WriteLine("X :: " + x + ", Y :: " + y); // Value changed
  216.  
  217. }
  218.  
  219. static int AddByValue(int x, int y)
  220. {
  221. int ans = x + y;
  222. x = 20;
  223. y = 40;
  224. return ans;
  225. }
  226.  
  227. static int AddByRef(ref int x, ref int y)
  228. {
  229. int ans = x + y;
  230. x = 20;
  231. y = 40;
  232. return ans;
  233. }
  234.  
  235.  
  236. }
  237.  
  238. SUM :: 60
  239. X :: 2, Y :: 20
  240.  
  241. SUM :: 60
  242. X :: 20, Y :: 40
Add Comment
Please, Sign In to add comment