code_junkie

Declaring variables within FOR loops

Nov 12th, 2011
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  1. for (int i =0; i< 10; i++)
  2. {
  3. decimal? testDecimal;
  4. string testString;
  5.  
  6. switch( i % 2 )
  7. {
  8. case 0:
  9. testDecimal = i / ( decimal ).32;
  10. testString = i.ToString();
  11. break;
  12. default:
  13. testDecimal = null;
  14. testString = null;
  15. break;
  16. }
  17.  
  18. Console.WriteLine( "Loop {0}: testDecimal={1} - testString={2}", i, testDecimal , testString );
  19. }
  20.  
  21. int value;
  22. for (int i = 0; i < 5; i++)
  23. {
  24. value = i;
  25. ThreadPool.QueueUserWorkItem(delegate { Console.WriteLine(value); });
  26. }
  27. Console.ReadLine();
  28.  
  29. for (int i = 0; i < 5; i++)
  30. {
  31. int value = i;
  32. ThreadPool.QueueUserWorkItem(delegate { Console.WriteLine(value); });
  33. }
  34. Console.ReadLine();
  35.  
  36. using System;
  37.  
  38. class A
  39. {
  40. public static void Main()
  41. {
  42. for (int i =0; i< 10; i++)
  43. {
  44. decimal? testDecimal;
  45. string testString;
  46. switch( i % 2 )
  47. {
  48. case 0:
  49. testDecimal = i / ( decimal ).32;
  50. testString = i.ToString();
  51. break;
  52. default:
  53. testDecimal = null;
  54. testString = null;
  55. break;
  56. }
  57.  
  58. Console.WriteLine( "Loop {0}: testDecimal={1} - testString={2}", i, testDecimal , testString );
  59. }
  60. }
  61. }
  62.  
  63. .method public hidebysig static void Main() cil managed
  64. {
  65. .entrypoint
  66. .maxstack 8
  67. .locals init (
  68. [0] int32 num,
  69. [1] valuetype [mscorlib]System.Nullable`1<valuetype [mscorlib]System.Decimal> nullable,
  70. [2] string str,
  71. [3] int32 num2,
  72. [4] bool flag)
  73. L_0000: nop
  74. L_0001: ldc.i4.0
  75. L_0002: stloc.0
  76. L_0003: br.s L_0061
  77. L_0005: nop
  78. L_0006: ldloc.0
  79. L_0007: ldc.i4.2
  80. L_0008: rem
  81. L_0009: stloc.3
  82. L_000a: ldloc.3
  83. L_000b: ldc.i4.0
  84. L_000c: beq.s L_0010
  85. L_000e: br.s L_0038
  86. L_0010: ldloca.s nullable
  87. L_0012: ldloc.0
  88. L_0013: call valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::op_Implicit(int32)
  89. L_0018: ldc.i4.s 0x20
  90. L_001a: ldc.i4.0
  91. L_001b: ldc.i4.0
  92. L_001c: ldc.i4.0
  93. L_001d: ldc.i4.2
  94. L_001e: newobj instance void [mscorlib]System.Decimal::.ctor(int32, int32, int32, bool, uint8)
  95. L_0023: call valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::op_Division(valuetype [mscorlib]System.Decimal, valuetype [mscorlib]System.Decimal)
  96. L_0028: call instance void [mscorlib]System.Nullable`1<valuetype [mscorlib]System.Decimal>::.ctor(!0)
  97. L_002d: nop
  98. L_002e: ldloca.s num
  99. L_0030: call instance string [mscorlib]System.Int32::ToString()
  100. L_0035: stloc.2
  101. L_0036: br.s L_0044
  102. L_0038: ldloca.s nullable
  103. L_003a: initobj [mscorlib]System.Nullable`1<valuetype [mscorlib]System.Decimal>
  104. L_0040: ldnull
  105. L_0041: stloc.2
  106. L_0042: br.s L_0044
  107. L_0044: ldstr "Loop {0}: testDecimal={1} - testString={2}"
  108. L_0049: ldloc.0
  109. L_004a: box int32
  110. L_004f: ldloc.1
  111. L_0050: box [mscorlib]System.Nullable`1<valuetype [mscorlib]System.Decimal>
  112. L_0055: ldloc.2
  113. L_0056: call void [mscorlib]System.Console::WriteLine(string, object, object, object)
  114. L_005b: nop
  115. L_005c: nop
  116. L_005d: ldloc.0
  117. L_005e: ldc.i4.1
  118. L_005f: add
  119. L_0060: stloc.0
  120. L_0061: ldloc.0
  121. L_0062: ldc.i4.s 10
  122. L_0064: clt
  123. L_0066: stloc.s flag
  124. L_0068: ldloc.s flag
  125. L_006a: brtrue.s L_0005
  126. L_006c: ret
  127. }
  128.  
  129. using System;
  130.  
  131. class A
  132. {
  133. public static void Main()
  134. {
  135. decimal? testDecimal;
  136. string testString;
  137.  
  138. for (int i =0; i< 10; i++)
  139. {
  140. switch( i % 2 )
  141. {
  142. case 0:
  143. testDecimal = i / ( decimal ).32;
  144. testString = i.ToString();
  145. break;
  146. default:
  147. testDecimal = null;
  148. testString = null;
  149. break;
  150. }
  151.  
  152. Console.WriteLine( "Loop {0}: testDecimal={1} - testString={2}", i, testDecimal , testString );
  153. }
  154. }
  155. }
  156.  
  157. .method public hidebysig static void Main() cil managed
  158. {
  159. .entrypoint
  160. .maxstack 8
  161. .locals init (
  162. [0] valuetype [mscorlib]System.Nullable`1<valuetype [mscorlib]System.Decimal> nullable,
  163. [1] string str,
  164. [2] int32 num,
  165. [3] int32 num2,
  166. [4] bool flag)
  167. L_0000: nop
  168. L_0001: ldc.i4.0
  169. L_0002: stloc.2
  170. L_0003: br.s L_0061
  171. L_0005: nop
  172. L_0006: ldloc.2
  173. L_0007: ldc.i4.2
  174. L_0008: rem
  175. L_0009: stloc.3
  176. L_000a: ldloc.3
  177. L_000b: ldc.i4.0
  178. L_000c: beq.s L_0010
  179. L_000e: br.s L_0038
  180. L_0010: ldloca.s nullable
  181. L_0012: ldloc.2
  182. L_0013: call valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::op_Implicit(int32)
  183. L_0018: ldc.i4.s 0x20
  184. L_001a: ldc.i4.0
  185. L_001b: ldc.i4.0
  186. L_001c: ldc.i4.0
  187. L_001d: ldc.i4.2
  188. L_001e: newobj instance void [mscorlib]System.Decimal::.ctor(int32, int32, int32, bool, uint8)
  189. L_0023: call valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::op_Division(valuetype [mscorlib]System.Decimal, valuetype [mscorlib]System.Decimal)
  190. L_0028: call instance void [mscorlib]System.Nullable`1<valuetype [mscorlib]System.Decimal>::.ctor(!0)
  191. L_002d: nop
  192. L_002e: ldloca.s num
  193. L_0030: call instance string [mscorlib]System.Int32::ToString()
  194. L_0035: stloc.1
  195. L_0036: br.s L_0044
  196. L_0038: ldloca.s nullable
  197. L_003a: initobj [mscorlib]System.Nullable`1<valuetype [mscorlib]System.Decimal>
  198. L_0040: ldnull
  199. L_0041: stloc.1
  200. L_0042: br.s L_0044
  201. L_0044: ldstr "Loop {0}: testDecimal={1} - testString={2}"
  202. L_0049: ldloc.2
  203. L_004a: box int32
  204. L_004f: ldloc.0
  205. L_0050: box [mscorlib]System.Nullable`1<valuetype [mscorlib]System.Decimal>
  206. L_0055: ldloc.1
  207. L_0056: call void [mscorlib]System.Console::WriteLine(string, object, object, object)
  208. L_005b: nop
  209. L_005c: nop
  210. L_005d: ldloc.2
  211. L_005e: ldc.i4.1
  212. L_005f: add
  213. L_0060: stloc.2
  214. L_0061: ldloc.2
  215. L_0062: ldc.i4.s 10
  216. L_0064: clt
  217. L_0066: stloc.s flag
  218. L_0068: ldloc.s flag
  219. L_006a: brtrue.s L_0005
  220. L_006c: ret
  221. }
  222.  
  223. Loop 0: testDecimal=0 - testString=0
  224. Loop 1: testDecimal= - testString=
  225. Loop 2: testDecimal=6.25 - testString=2
  226. Loop 3: testDecimal= - testString=
  227. Loop 4: testDecimal=12.5 - testString=4
  228. Loop 5: testDecimal= - testString=
  229. Loop 6: testDecimal=18.75 - testString=6
  230. Loop 7: testDecimal= - testString=
  231. Loop 8: testDecimal=25 - testString=8
  232. Loop 9: testDecimal= - testString=
  233.  
  234. private void LoopTest()
  235. {
  236. for (int i =0; i< 10; i++)
  237. {
  238. DoWork(i);
  239. }
  240. }
  241.  
  242. private void Work(int i)
  243. {
  244. decimal? testDecimal;
  245. string testString;
  246.  
  247. switch (i % 2)
  248. {
  249. case 0:
  250. testDecimal = i / (decimal).32;
  251. testString = i.ToString();
  252. break;
  253. default:
  254. testDecimal = null;
  255. testString = null;
  256. break;
  257. }
  258. Console.WriteLine( "Loop {0}: testDecimal={1} - testString={2}", i, testDecimal , testString );
  259. }
  260.  
  261. decimal? testDecimal;
  262. string testString;
  263.  
  264. for (int i =0; i< 10; i++)
  265. {
  266. switch( i % 2 )
  267. {
  268. case 0:
  269. testDecimal = i / ( decimal ).32;
  270. testString = i.ToString();
  271. Console.WriteLine( "Loop {0}: testDecimal={1} - testString={2}", i, testDecimal , testString );
  272. testDecimal = null;
  273. testString = String.Empty;
  274. break;
  275. default:
  276. testDecimal = null;
  277. testString = String.Empty;
  278. break;
  279. }
  280.  
  281. }
Add Comment
Please, Sign In to add comment