I recently started coding in C# and writing ASP.NET MVC apps. I was doing something similar to this:
var hashtable = new Hashtable();
hashtable.Add("First", 12);
hashtable.Add("Second", 12);
if(hashtable["First"] == hashtable["Second"])
{
Console.WriteLine("Equal");
}
else{
Console.WriteLine("Not Equal");
}
It always outputs Not Equal
. A while later I realized it was the effect of boxing. However, if I write the following:
var hashtable = new Hashtable();
hashtable.Add("First", 12);
hashtable.Add("Second", 12);
dynamic ViewBag = new System.Dynamic.ExpandoObject();
ViewBag.hashtable = hashtable;
if(ViewBag.hashtable["First"] == ViewBag.hashtable["Second"])
{
Console.WriteLine("Equal");
}
else
{
Console.WriteLine("Not Equal");
}
It always outputs Equal
.
What is the underlying mechanism that is causing this difference?