Is it faster to copy reference to object from dictionary or access it directly from dictionary?
c#, dictionary, performance
Solution
Yep, you are correct. Your first approach does the dictionary lookup 4 times, while the second does it once. The second is definitely better.
However, in real life, a dictionary lookup is ridiculously fast, so unless you've got a massive dictionary the difference won't be noticeable, maybe not even measurable.
Problem
Question is simple is this code ``` public Dictionary<string, SomeObject> values = new Dictionary<string, SomeObject>(); void Function() { values["foo"].a = "bar a"; values["foo"].b = "bar b"; values["foo"].c = "bar c"; values["foo"].d = "bar d"; } ``` same fast as this code ``` public Dictionary<string, SomeObject> values = new Dictionary<string, SomeObject>(); void Function() { var someObject = values["foo"]; someObject.a = "bar a"; someObject.b = "bar b"; someObject.c = "bar c"; someObject.d = "bar d"; } ``` common sense tell me that it should be faster to look up the reference in dictionary once and store it somewhere so that it doesn't need to be looked up multiple times, but I don't really know how dictionary works. So is it faster or not? And why?