Concurrent Dictionary AddOrUpdate vs Index Add

This is an old question, but no one answered why you would use one over the other.

Choose A (the indexer) if you want to add or update and the update is not dependent on the existing value.

Choose B (AddOrUpdate) if you want to add or update and the update depends on an existing value. AddOrUpdate will do the update atomically.

So in the case in the question, you want to use the indexer. It’s simpler, easier to read, and probably faster since you aren’t creating an anonymous function.

Leave a Comment