C# Coding Challenge: What will this code do?
time to read 2 min | 218 words
What is the output of this code?
IDictionary<object,string> instanceToKey = new Dictionary<object, string>(); IDictionary<string, int> keyToCost = new Dictionary<string, int>(); var inst1 = new object(); var inst2 = new object(); instanceToKey[inst1] = "one"; keyToCost["one"] = 1; instanceToKey[inst2] = "two"; keyToCost["two"] = 2; string value = null; foreach (var key in (from inst in new[]{inst1, inst2, new object()} where instanceToKey.TryGetValue(inst, out value) select value)) { int cost; if(keyToCost.TryGetValue(key, out cost)) Console.WriteLine("Key: {0}, Cost: {1}", key, cost); }

Comments
Comment preview