The best way to get the value is
bool Dictionary<Key, Value>.TryGetValue(Key key, out Value value);
It will return a boolean value to determine if key present, and value correctly referenced or not.
This method is fast, since you only exit value when the key is displayed, so multiple hashing and dictionary searches are avoided.
So your code will be:
private Dictionary<String, Tile> tiles = new Dictionary<String, Tile>(); Tile outValue; if(tiles.TryGetValue( x + ":" + y, out outValue)) { Console.WriteLine("I have this: " + outValue.ToString()); } else { Console.WriteLine("I have nothing"); }
See MSDN .
source share