Open sharwell opened 8 years ago
These are not equivalent as map.get(key)
is an expression and
T value;
map.TryGetValue(key, out value);
Is a statement block. The correct way would be to map this operation to the following extension method:
public V TryGet<K, V>(this IDictionarry<K,V> map, K key)
{
V value;
map.TryGetValue(key);
return value;
}
Or in latest C#: map.TryGetValue(key, out var value) ? value : default(T)
In Java,
Map.get
returnsnull
when an element does not exist. In C#, the get indexer forIDictionary<TKey, TValue>
throws an exception in the same scenario. Currently calls toget
are getting translated into indexer usage, but this breaks the semantics for this operation. The correct code for this case would be the following:Java:
C#: