User Tools

Site Tools


notes:csharp:collections

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
notes:csharp:collections [2017/07/28]
leszek [Concurrent collections]
notes:csharp:collections [2019/07/17] (current)
leszek [Dictionary]
Line 143: Line 143:
  
  
 +//​GetOrAdd//​ and //​TryRemove//​ extension methods:
 +<code csharp>
 +static class SomeExtensions
 +{
 +    public static V GetOrAdd<​K,​ V>(this Dictionary<​K,​ V> dict, K key, V val)
 +    {
 +        if (dict.TryGetValue(key,​ out var v))
 +            return v;
 +
 +        dict.Add(key,​ val);
 +
 +        return val;
 +    }
 +
 +    public static bool TryRemove<​K,​ V>(this Dictionary<​K,​ V> dict, K key)
 +    {
 +        if (dict.TryGetValue(key,​ out var v))
 +        {
 +            dict.Remove(key);​
 +            return true;
 +        }
 +        else
 +        {
 +            return false;
 +        }
 +    }
 +}
 +</​code>​
  
 ====Queue==== ====Queue====
notes/csharp/collections.txt ยท Last modified: 2019/07/17 by leszek