﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Chris Smith commented on I love ConcurrentDictionary!</title><description>This looks something like Redis ( 
[http://code.google.com/p/redis/](http://code.google.com/p/redis/) ), which I just happen to be building some .Net client software for here: 
[http://code.google.com/p/redisdotnet/](http://code.google.com/p/redisdotnet/)  
  
That allows a vast number of atomic thread-safe actions to be done on a remote dictionary.
</description><link>http://ayende.com/4442/i-love-concurrentdictionary#comment8</link><guid>http://ayende.com/4442/i-love-concurrentdictionary#comment8</guid><pubDate>Fri, 26 Mar 2010 12:12:26 GMT</pubDate></item><item><title>tobi commented on I love ConcurrentDictionary!</title><description>Although it is beside your point you use a single dictionary with a tuple as the key.
</description><link>http://ayende.com/4442/i-love-concurrentdictionary#comment7</link><guid>http://ayende.com/4442/i-love-concurrentdictionary#comment7</guid><pubDate>Mon, 22 Mar 2010 13:43:49 GMT</pubDate></item><item><title>configurator commented on I love ConcurrentDictionary!</title><description>Shouldn't you use some concurrent datatype for the list as well?
</description><link>http://ayende.com/4442/i-love-concurrentdictionary#comment6</link><guid>http://ayende.com/4442/i-love-concurrentdictionary#comment6</guid><pubDate>Mon, 22 Mar 2010 12:25:17 GMT</pubDate></item><item><title>Robert Byrne commented on I love ConcurrentDictionary!</title><description>Yea some of the first extension methods I wrote were to deal with dictionaries. If theres nothing special about the way you construct the value, you can simplify slightly:
  
  
public static TValue GetOrAddDefaultValue&lt;TKey, TValue&gt;(this IDictionary&lt;TKey, TValue&gt; dictionary, TKey key) {
  
    if (dictionary == null) return default(TValue);
  
  
    TValue value = default(TValue);
  
    bool exists = dictionary.TryGetValue(key, out value);
  
  
    if (!exists) {
  
        if (typeof(TValue).IsValueType) {
  
            dictionary[key] = value;
  
        }
  
        else {
  
            value = Activator.CreateInstance&lt;TValue&gt;();
  
            dictionary[key] = value;
  
        }
  
    }
  
  
    return value;
  
}
</description><link>http://ayende.com/4442/i-love-concurrentdictionary#comment5</link><guid>http://ayende.com/4442/i-love-concurrentdictionary#comment5</guid><pubDate>Mon, 22 Mar 2010 11:30:24 GMT</pubDate></item><item><title>Paul Hatcher commented on I love ConcurrentDictionary!</title><description>You'd need some form of locking to make it thread safe, e.g. 
  
  
TValue value;
  
lock (collection)
  
{
  
   if (!collection.TryGetValue(key, out value))
  
   collection.Add(key, value = generator(key));
  
}
  
return value;
  
  
And of course this doesn't preclude someone bypassing the lock by directly doing collection.Add
</description><link>http://ayende.com/4442/i-love-concurrentdictionary#comment4</link><guid>http://ayende.com/4442/i-love-concurrentdictionary#comment4</guid><pubDate>Mon, 22 Mar 2010 11:24:01 GMT</pubDate></item><item><title>Torkel commented on I love ConcurrentDictionary!</title><description>That is a pretty useful function! 
  
  
Dam, why did I not think of writing one like that. I do have a similar one I use for caches. 
  
  
For example:
  
return cache.GetWithAdd("admembers", () =&gt; GetADMemberListRemote());
</description><link>http://ayende.com/4442/i-love-concurrentdictionary#comment3</link><guid>http://ayende.com/4442/i-love-concurrentdictionary#comment3</guid><pubDate>Mon, 22 Mar 2010 11:19:02 GMT</pubDate></item><item><title>Tommy Carlier commented on I love ConcurrentDictionary!</title><description>Your commenting system apparently doesn't escape less-than and greater-than. Second attempt:
  
static TValue GetOrAdd&lt;TKey, TValue&gt;(this IDictionary&lt;TKey, TValue&gt; collection, TKey key, Func&lt;TKey, TValue&gt; generator)
  
{
  
  TValue value;
  
  if (!collection.TryGetValue(key, out value))
  
    collection.Add(key, value = generator(key));
  
  return value;
  
}
</description><link>http://ayende.com/4442/i-love-concurrentdictionary#comment2</link><guid>http://ayende.com/4442/i-love-concurrentdictionary#comment2</guid><pubDate>Mon, 22 Mar 2010 10:33:36 GMT</pubDate></item><item><title>Tommy Carlier commented on I love ConcurrentDictionary!</title><description>That's awesome. You could do this with regular dictionaries, by adding an extension method to IDictionary
&lt;tkey,&gt;
, like this:
  
  
static TValue GetOrAdd
&lt;tkey,&gt;
(this IDictionary
&lt;tkey,&gt;
 collection, TKey key, Func
&lt;tkey,&gt;
 generator)
  
{
  
  TValue value;
  
  if (!collection.TryGetValue(key, out value))
  
    collection.Add(key, value = generator(key));
  
  return value;
  
}
</description><link>http://ayende.com/4442/i-love-concurrentdictionary#comment1</link><guid>http://ayende.com/4442/i-love-concurrentdictionary#comment1</guid><pubDate>Mon, 22 Mar 2010 10:31:43 GMT</pubDate></item></channel></rss>