Below you'll find a small program. The challenge is to predict the output of the program, then explain why it gave the output it did.
using System;
using System.Collections.Generic;
namespace Sample {
class Program {
static void Main(string[] args) {
MyStruct a;
a.a = 1;
a.b = 2;
Dictionary<MyStruct, int> dic = new Dictionary<MyStruct, int>();
dic.Add(a, 1);
Console.WriteLine("{0}", dic[a]);
a.a = 3;
Console.WriteLine("{0}", dic[a]);
Console.ReadLine();
}
}
struct MyStruct {
public int a;
public int b;
}
}
Before you run off to Microsoft to report a bug, this isn't one. It's working as intended. The challenge is to explain why it is working as intended :)