Is this the correct usage of ConcurrentDictionary? I need to make a simple class that tracks how many clients are listening to a specific item, will be referred to as building hence forth.  I will have a timer running that will check which buildings are active and send updates to them.  While this is occurring, the client will be able connect and disconnect.  This means they will also be modifying the building list as time goes on.

To make sure that I don't have any concurrency issues, I decided to make a simple class that uses a ConcurrencyDictionary&lt;TKey, TValue&gt;.  I don't have a need of the class in the past and want to make sure it is being used correctly as the documentation seems to lack a bit for this class, mostly when it comes to updating values and the like.

/// &lt;summary&gt;
/// Tracks the buildings that are actively being listened to and the number of clients subscribed to the building
/// &lt;/summary&gt;
public static class BuildingSubscriptionTracker
{
    /// &lt;summary&gt;
    /// A dictionary of building name and listening client count
    /// &lt;/summary&gt;
    private static readonly ConcurrentDictionary&lt;string, int&gt; _buildingsSubscribedTo = new ConcurrentDictionary&lt;string, int&gt;();

    /// &lt;summary&gt;
    /// Get all the buildings that are actively being listened to by clients
    /// &lt;/summary&gt;
    /// &lt;returns&gt;An &lt;see cref="System.Collections.Generic.IEnumerable{T}"/&gt; of string&lt;/returns&gt;
    public static IEnumerable&lt;string&gt; GetBuildings()
    {
        return _buildingsSubscribedTo.Keys;
    }

    /// &lt;summary&gt;
    /// Get how many clients are listening to updates for this building
    /// &lt;/summary&gt;
    /// &lt;param name="buildingName"&gt;Name of building to get client listening count for&lt;/param&gt;
    /// &lt;returns&gt;A count of how many clients are listening to updates for this building.  If the building is no longer being tracked, this will return 0&lt;/returns&gt;
    public static int GetActiveClientCountFor(string buildingName)
    {
        int clientCount;

        if (_buildingsSubscribedTo.ContainsKey(buildingName))
            if (_buildingsSubscribedTo.TryGetValue(buildingName, out clientCount))
                return clientCount;

        return 0;
    }

    /// &lt;summary&gt;
    /// Increment the client count of the given building
    /// &lt;/summary&gt;
    /// &lt;param name="buildingName"&gt;Name of the building the client wants updates for&lt;/param&gt;
    public static void IncrementClientCountFor(string buildingName)
    {
        //If the building doesn't exist, add it and +1 the count
        if (!_buildingsSubscribedTo.ContainsKey(buildingName))
            _buildingsSubscribedTo.TryAdd(buildingName, 0);

        int clientCount;

        _buildingsSubscribedTo.TryGetValue(buildingName, out clientCount);

        _buildingsSubscribedTo.TryUpdate(buildingName, clientCount++, clientCount);
    }

    /// &lt;summary&gt;
    /// Decrements the client count of the given building
    /// &lt;/summary&gt;
    /// &lt;param name="buildingName"&gt;Name of the building the client wants updates for&lt;/param&gt;
    public static void DecrementClientCountFor(string buildingName)
    {
        //If the building doesn't exist, there isn't anything to do
        if (_buildingsSubscribedTo.ContainsKey(buildingName))
        {
            int clientCount;

            //Get how many clients are listening to building so we can properly update it
            if (_buildingsSubscribedTo.TryGetValue(buildingName, out clientCount))
            {
                var newClientCount = clientCount--;

                if (newClientCount &lt; 1)
                {
                    int unusedValue;
                    _buildingsSubscribedTo.TryRemove(buildingName, out unusedValue);
                }
                else
                {
                    _buildingsSubscribedTo.TryUpdate(buildingName, newClientCount, clientCount);
                }
            }
        }
    }
}
There isn't a lot of point of checking ContainsKey.  A thread could sneak in between the ContainsKey and Try* methods you are calling.  The TryGetValue out parameter will be initialized to the default value if it can't find the key.

The GetActiveClientCountFor could become this.

public static int GetActiveClientCountFor(string buildingName)
{
    int clientCount;
    _buildingsSubscribedTo.TryGetValue(buildingName, out clientCount);
    return clientCount;
}


In the IncrementClientCountFor code the value is retrieved and then added to.  If someone came in and updated the value the TryUpdate would fail as the count changed.  

What should be used is the AddOrUpdate method.  If the key doesn't exist it will be created and set to one.  If it does exist it will call the update method.  The update method could be called multiple times if the value changed before it could be set and will be set to the last good value. 

public static void IncrementClientCountFor(string buildingName)
{
    _buildingsSubscribedTo.AddOrUpdate(buildingName, 1, (s, i) =&gt; i++);
}


Same problem with DecrementClientCountFo.  The value is retrieved and checked if less than one.  But again a thread could add to dictionary and the code would remove it from the dictionary with a positive count.  To get an Atomic TryRemove I would create an extension method called TryRemove taken from http://blogs.msdn.com/b/pfxteam/archive/2011/04/02/10149222.aspx

public static class ConcurrentDictionaryExtensions
{
    //http://blogs.msdn.com/b/pfxteam/archive/2011/04/02/10149222.aspx
    public static bool TryRemove&lt;TKey, TValue&gt;(
        this ConcurrentDictionary&lt;TKey, TValue&gt; dictionary, TKey key, TValue value)
    {
        if (dictionary == null) throw new ArgumentNullException("dictionary");
        return ((ICollection&lt;KeyValuePair&lt;TKey, TValue&gt;&gt;) dictionary).Remove(
            new KeyValuePair&lt;TKey, TValue&gt;(key, value));
    }
}


Now change DecrementClientCountFor to use AddOrUpdate then the Atomic TryRemove

public static void DecrementClientCountFor(string buildingName)
{
    var buildings = _buildingsSubscribedTo.AddOrUpdate(buildingName, s =&gt; 0, (s, i) =&gt; i--);
    if (buildings &lt; 1)
    {
        _buildingsSubscribedTo.TryRemove(buildingName, buildings);
    }
}


If the value was changed from underneath us after getting the value the TryRemove will fail.  Which is ok since it could be positive or another thread could have removed the key already.