public string[] Tables = new string[] { "One", "Two", "Three" }; >>>> >>>> >>>> public void UseTables() >>>> { >>>> foreach (string s in Tables) >>>> { >>>> //Do something >>>> System.Threading.Thread.Sleep(1000); >>>> } >>>> } >>>> >>>> public void LoadDataDictionary() >>>> { >>>> string[] tables = new string[]{"One","Two"}; >>>> Tables = tables; >>>> } >>>>If, for example, LoadDataDictionary() runs on one thread whilst UseTables() is running on another you've got problems...
>>> public class AClass >>> { >>> public static string[] Tables; >>> >>> public static void Main() >>> { >>> TestWithEnumerator(); >>> TestWithForEach(); >>> } >>> >>> static void TestWithEnumerator() >>> { >>> InitializeTables(); >>> var enumerator = Tables.GetEnumerator(); >>> while (enumerator.MoveNext()) >>> { >>> Console.WriteLine(enumerator.Current); >>> ReInitializeTables(); >>> } >>> Console.ReadLine(); >>> } >>> static void TestWithForEach() >>> { >>> InitializeTables(); >>> foreach (var x in Tables ) >>> { >>> Console.WriteLine(x); >>> ReInitializeTables(); >>> } >>> Console.ReadLine(); >>> } >>> >>> public static void InitializeTables() >>> { >>> string[] tables = new string[] { "one", "two", "three" }; >>> Tables = tables; >>> } >>> >>> public static void ReInitializeTables() >>> { >>> string[] tables = new string[] { "four", "five" }; >>> Tables = tables; >>> } >>> } >>>>>>
>>>.method private hidebysig static void TestWithForEach() cil managed >>>{ >>> // Code size 58 (0x3a) >>> .maxstack 2 >>> .locals init ([0] string x, >>> [1] string[] CS$6$0000, >>> [2] int32 CS$7$0001, >>> [3] bool CS$4$0002) >>> IL_0000: nop >>> IL_0001: call void CoreTest.AClass::InitializeTables() >>> IL_0006: nop >>> IL_0007: nop >>> IL_0008: ldsfld string[] CoreTest.AClass::Tables loads static field Tables on the stack >>> IL_000d: stloc.1 and stores it locally >>> IL_000e: ldc.i4.0 >>> IL_000f: stloc.2 >>> IL_0010: br.s IL_0029 >>> IL_0012: ldloc.1 >>> IL_0013: ldloc.2 >>> IL_0014: ldelem.ref >>> IL_0015: stloc.0 >>> IL_0016: nop >>> IL_0017: ldloc.0 >>> IL_0018: call void [mscorlib]System.Console::WriteLine(string) >>> IL_001d: nop >>> IL_001e: call void CoreTest.AClass::ReInitializeTables() >>> IL_0023: nop >>> IL_0024: nop >>> IL_0025: ldloc.2 >>> IL_0026: ldc.i4.1 >>> IL_0027: add >>> IL_0028: stloc.2 >>> IL_0029: ldloc.2 >>> IL_002a: ldloc.1 >>> IL_002b: ldlen >>> IL_002c: conv.i4 >>> IL_002d: clt >>> IL_002f: stloc.3 >>> IL_0030: ldloc.3 >>> IL_0031: brtrue.s IL_0012 >>> IL_0033: call string [mscorlib]System.Console::ReadLine() >>> IL_0038: pop >>> IL_0039: ret >>>} // end of method AClass::TestWithForEach >>>Interesting. It's cleverer than I thought. But :
static void TestWithForEach() >> { >> int l = Tables.Length; >> ReInitializeTables(); >> Console.WriteLine(Tables[l]); >> >> Console.ReadLine(); >> }>