{"action":"created","comment":{"url":"https://api.github.com/repos/dotnet/core-docs/pulls/comments/58606700","id":58606700,"diff_hunk":"@@ -1 +1,285 @@\n+# How to: Add and Remove Items from a ConcurrentDictionary\n+\n+This example shows how to add, retrieve, update, and remove items from a [System.Collections.Concurrent.ConcurrentDictionary&lt;TKey, TValue&gt;](http://dotnet.github.io/api/System.Collections.Concurrent.ConcurrentDictionary%602.html). This collection class is a thread-safe implementation. We recommend that you use it whenever multiple threads might be attempting to access the elements concurrently.\n+\n+**ConcurrentDictionary&lt;TKey, TValue&gt;** provides several convenience methods that make it unnecessary for code to first check whether a key exists before it attempts to add or remove data. The following table lists these convenience methods and describes when to use them.\n+\n+Method | Use when...\n+------ | -----------\n+**AddOrUpdate** | You want to add a new value for a specified key and, if the key already exists, you want to replace its value.\n+**GetOrAdd** | You want to retrieve the existing value for a specified key and, if the key does not exist, you want to specify a key/value pair.\n+**TryAdd**, **TryGetValue**, **TryUpdate**, **TryRemove** | You want to add, get, update, or remove a key/value pair, and, if the key already exists or the attempt fails for any other reason, you want to take some alternative action.\n+\n+## Example\n+\n+```csharp\n+namespace DictionaryHowTo\n+{\n+    using System;\n+    using System.Collections.Concurrent;\n+    using System.Collections.Generic;\n+    using System.Linq;\n+    using System.Text;\n+    using System.Threading;\n+    using System.Threading.Tasks;\n+\n+    // The type of the Value to store in the dictionary:\n+    class CityInfo : IEqualityComparer<CityInfo>\n+    {\n+        public string Name { get; set; }\n+        public DateTime lastQueryDate { get; set; }\n+        public decimal Longitude { get; set; }\n+        public decimal Latitude { get; set; }\n+        public int[] RecentHighTemperatures { get; set; }\n+\n+        public CityInfo(string name, decimal longitude, decimal latitude, int[] temps)\n+        {\n+            Name = name;\n+            lastQueryDate = DateTime.Now;\n+            Longitude = longitude;\n+            Latitude = latitude;\n+            RecentHighTemperatures = temps;\n+        }\n+\n+        public CityInfo()\n+        {\n+        }\n+\n+        public CityInfo(string key)\n+        {\n+            Name = key;\n+            // MaxValue means \"not initialized\"\n+            Longitude = Decimal.MaxValue;\n+            Latitude = Decimal.MaxValue;\n+            lastQueryDate = DateTime.Now;\n+            RecentHighTemperatures = new int[] { 0 };\n+\n+        }\n+        public bool Equals(CityInfo x, CityInfo y)\n+        {\n+            return x.Name == y.Name && x.Longitude == y.Longitude && x.Latitude == y.Latitude;\n+        }\n+\n+        public int GetHashCode(CityInfo obj)\n+        {\n+            CityInfo ci = (CityInfo)obj;\n+            return ci.Name.GetHashCode();\n+        }\n+    }\n+\n+    class Program\n+    {\n+        // Create a new concurrent dictionary.\n+        static ConcurrentDictionary<string, CityInfo> cities = new ConcurrentDictionary<string, CityInfo>();\n+\n+        static void Main(string[] args)\n+        {\n+            CityInfo[] data = \n+            {\n+                new CityInfo(){ Name = \"Boston\", Latitude = 42.358769M, Longitude = -71.057806M, RecentHighTemperatures = new int[] {56, 51, 52, 58, 65, 56,53}},\n+                new CityInfo(){ Name = \"Miami\", Latitude = 25.780833M, Longitude = -80.195556M, RecentHighTemperatures = new int[] {86,87,88,87,85,85,86}},\n+                new CityInfo(){ Name = \"Los Angeles\", Latitude = 34.05M, Longitude = -118.25M, RecentHighTemperatures =   new int[] {67,68,69,73,79,78,78}},\n+                new CityInfo(){ Name = \"Seattle\", Latitude = 47.609722M, Longitude =  -122.333056M, RecentHighTemperatures =   new int[] {49,50,53,47,52,52,51}},\n+                new CityInfo(){ Name = \"Toronto\", Latitude = 43.716589M, Longitude = -79.340686M, RecentHighTemperatures =   new int[] {53,57, 51,52,56,55,50}},\n+                new CityInfo(){ Name = \"Mexico City\", Latitude = 19.432736M, Longitude = -99.133253M, RecentHighTemperatures =   new int[] {72,68,73,77,76,74,73}},\n+                new CityInfo(){ Name = \"Rio de Janiero\", Latitude = -22.908333M, Longitude = -43.196389M, RecentHighTemperatures =   new int[] {72,68,73,82,84,78,84}},\n+                new CityInfo(){ Name = \"Quito\", Latitude = -0.25M, Longitude = -78.583333M, RecentHighTemperatures =   new int[] {71,69,70,66,65,64,61}}\n+            };\n+\n+            // Add some key/value pairs from multiple threads.\n+            Task[] tasks = new Task[2];","path":"docs/essentials/collections/threadsafe/how toAddandRemoveItems.md","position":90,"original_position":90,"commit_id":"a3dcc4de576af3ee9c12b6291359d1caab3a4aa5","original_commit_id":"a3dcc4de576af3ee9c12b6291359d1caab3a4aa5","user":{"login":"cartermp","id":6309070,"avatar_url":"https://avatars.githubusercontent.com/u/6309070?v=3","gravatar_id":"","url":"https://api.github.com/users/cartermp","html_url":"https://github.com/cartermp","followers_url":"https://api.github.com/users/cartermp/followers","following_url":"https://api.github.com/users/cartermp/following{/other_user}","gists_url":"https://api.github.com/users/cartermp/gists{/gist_id}","starred_url":"https://api.github.com/users/cartermp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cartermp/subscriptions","organizations_url":"https://api.github.com/users/cartermp/orgs","repos_url":"https://api.github.com/users/cartermp/repos","events_url":"https://api.github.com/users/cartermp/events{/privacy}","received_events_url":"https://api.github.com/users/cartermp/received_events","type":"User","site_admin":false},"body":"Maybe this is something to track in another issue and fix there, but I don't like the use of a magic number here to partition the work across threads filling up the dictionary.  I'd be okay with merging this for now just for the sake of getting content in the docs, though.","created_at":"2016-04-05T20:09:05Z","updated_at":"2016-04-05T20:09:05Z","html_url":"https://github.com/dotnet/core-docs/pull/381#discussion_r58606700","pull_request_url":"https://api.github.com/repos/dotnet/core-docs/pulls/381","_links":{"self":{"href":"https://api.github.com/repos/dotnet/core-docs/pulls/comments/58606700"},"html":{"href":"https://github.com/dotnet/core-docs/pull/381#discussion_r58606700"},"pull_request":{"href":"https://api.github.com/repos/dotnet/core-docs/pulls/381"}}},"pull_request":{"url":"https://api.github.com/repos/dotnet/core-docs/pulls/381","id":64367068,"html_url":"https://github.com/dotnet/core-docs/pull/381","diff_url":"https://github.com/dotnet/core-docs/pull/381.diff","patch_url":"https://github.com/dotnet/core-docs/pull/381.patch","issue_url":"https://api.github.com/repos/dotnet/core-docs/issues/381","number":381,"state":"open","locked":false,"title":"How to: Add and Remove Items from a ConcurrentDictionary","user":{"login":"stevehoag","id":13489215,"avatar_url":"https://avatars.githubusercontent.com/u/13489215?v=3","gravatar_id":"","url":"https://api.github.com/users/stevehoag","html_url":"https://github.com/stevehoag","followers_url":"https://api.github.com/users/stevehoag/followers","following_url":"https://api.github.com/users/stevehoag/following{/other_user}","gists_url":"https://api.github.com/users/stevehoag/gists{/gist_id}","starred_url":"https://api.github.com/users/stevehoag/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stevehoag/subscriptions","organizations_url":"https://api.github.com/users/stevehoag/orgs","repos_url":"https://api.github.com/users/stevehoag/repos","events_url":"https://api.github.com/users/stevehoag/events{/privacy}","received_events_url":"https://api.github.com/users/stevehoag/received_events","type":"User","site_admin":false},"body":"Fixed list and <> formatting","created_at":"2016-03-28T20:00:48Z","updated_at":"2016-04-05T20:09:05Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b4ec1ac7633148c3dda4ac9ab2485d5c955ae179","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/dotnet/core-docs/pulls/381/commits","review_comments_url":"https://api.github.com/repos/dotnet/core-docs/pulls/381/comments","review_comment_url":"https://api.github.com/repos/dotnet/core-docs/pulls/comments{/number}","comments_url":"https://api.github.com/repos/dotnet/core-docs/issues/381/comments","statuses_url":"https://api.github.com/repos/dotnet/core-docs/statuses/a3dcc4de576af3ee9c12b6291359d1caab3a4aa5","head":{"label":"dotnet:32816l","ref":"32816l","sha":"a3dcc4de576af3ee9c12b6291359d1caab3a4aa5","user":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"repo":{"id":35890081,"name":"core-docs","full_name":"dotnet/core-docs","owner":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dotnet/core-docs","description":".NET Core documentation (work in progress).","fork":false,"url":"https://api.github.com/repos/dotnet/core-docs","forks_url":"https://api.github.com/repos/dotnet/core-docs/forks","keys_url":"https://api.github.com/repos/dotnet/core-docs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dotnet/core-docs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dotnet/core-docs/teams","hooks_url":"https://api.github.com/repos/dotnet/core-docs/hooks","issue_events_url":"https://api.github.com/repos/dotnet/core-docs/issues/events{/number}","events_url":"https://api.github.com/repos/dotnet/core-docs/events","assignees_url":"https://api.github.com/repos/dotnet/core-docs/assignees{/user}","branches_url":"https://api.github.com/repos/dotnet/core-docs/branches{/branch}","tags_url":"https://api.github.com/repos/dotnet/core-docs/tags","blobs_url":"https://api.github.com/repos/dotnet/core-docs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dotnet/core-docs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dotnet/core-docs/git/refs{/sha}","trees_url":"https://api.github.com/repos/dotnet/core-docs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dotnet/core-docs/statuses/{sha}","languages_url":"https://api.github.com/repos/dotnet/core-docs/languages","stargazers_url":"https://api.github.com/repos/dotnet/core-docs/stargazers","contributors_url":"https://api.github.com/repos/dotnet/core-docs/contributors","subscribers_url":"https://api.github.com/repos/dotnet/core-docs/subscribers","subscription_url":"https://api.github.com/repos/dotnet/core-docs/subscription","commits_url":"https://api.github.com/repos/dotnet/core-docs/commits{/sha}","git_commits_url":"https://api.github.com/repos/dotnet/core-docs/git/commits{/sha}","comments_url":"https://api.github.com/repos/dotnet/core-docs/comments{/number}","issue_comment_url":"https://api.github.com/repos/dotnet/core-docs/issues/comments{/number}","contents_url":"https://api.github.com/repos/dotnet/core-docs/contents/{+path}","compare_url":"https://api.github.com/repos/dotnet/core-docs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dotnet/core-docs/merges","archive_url":"https://api.github.com/repos/dotnet/core-docs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dotnet/core-docs/downloads","issues_url":"https://api.github.com/repos/dotnet/core-docs/issues{/number}","pulls_url":"https://api.github.com/repos/dotnet/core-docs/pulls{/number}","milestones_url":"https://api.github.com/repos/dotnet/core-docs/milestones{/number}","notifications_url":"https://api.github.com/repos/dotnet/core-docs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dotnet/core-docs/labels{/name}","releases_url":"https://api.github.com/repos/dotnet/core-docs/releases{/id}","deployments_url":"https://api.github.com/repos/dotnet/core-docs/deployments","created_at":"2015-05-19T15:13:32Z","updated_at":"2016-04-05T18:53:01Z","pushed_at":"2016-04-05T19:33:47Z","git_url":"git://github.com/dotnet/core-docs.git","ssh_url":"git@github.com:dotnet/core-docs.git","clone_url":"https://github.com/dotnet/core-docs.git","svn_url":"https://github.com/dotnet/core-docs","homepage":"http://dotnet.github.io/docs/index.html","size":14230,"stargazers_count":67,"watchers_count":67,"language":"CSS","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":78,"mirror_url":null,"open_issues_count":132,"forks":78,"open_issues":132,"watchers":67,"default_branch":"master"}},"base":{"label":"dotnet:master","ref":"master","sha":"9708055d7c046fcb66ff5fb6da2f619d15440522","user":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"repo":{"id":35890081,"name":"core-docs","full_name":"dotnet/core-docs","owner":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dotnet/core-docs","description":".NET Core documentation (work in progress).","fork":false,"url":"https://api.github.com/repos/dotnet/core-docs","forks_url":"https://api.github.com/repos/dotnet/core-docs/forks","keys_url":"https://api.github.com/repos/dotnet/core-docs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dotnet/core-docs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dotnet/core-docs/teams","hooks_url":"https://api.github.com/repos/dotnet/core-docs/hooks","issue_events_url":"https://api.github.com/repos/dotnet/core-docs/issues/events{/number}","events_url":"https://api.github.com/repos/dotnet/core-docs/events","assignees_url":"https://api.github.com/repos/dotnet/core-docs/assignees{/user}","branches_url":"https://api.github.com/repos/dotnet/core-docs/branches{/branch}","tags_url":"https://api.github.com/repos/dotnet/core-docs/tags","blobs_url":"https://api.github.com/repos/dotnet/core-docs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dotnet/core-docs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dotnet/core-docs/git/refs{/sha}","trees_url":"https://api.github.com/repos/dotnet/core-docs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dotnet/core-docs/statuses/{sha}","languages_url":"https://api.github.com/repos/dotnet/core-docs/languages","stargazers_url":"https://api.github.com/repos/dotnet/core-docs/stargazers","contributors_url":"https://api.github.com/repos/dotnet/core-docs/contributors","subscribers_url":"https://api.github.com/repos/dotnet/core-docs/subscribers","subscription_url":"https://api.github.com/repos/dotnet/core-docs/subscription","commits_url":"https://api.github.com/repos/dotnet/core-docs/commits{/sha}","git_commits_url":"https://api.github.com/repos/dotnet/core-docs/git/commits{/sha}","comments_url":"https://api.github.com/repos/dotnet/core-docs/comments{/number}","issue_comment_url":"https://api.github.com/repos/dotnet/core-docs/issues/comments{/number}","contents_url":"https://api.github.com/repos/dotnet/core-docs/contents/{+path}","compare_url":"https://api.github.com/repos/dotnet/core-docs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dotnet/core-docs/merges","archive_url":"https://api.github.com/repos/dotnet/core-docs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dotnet/core-docs/downloads","issues_url":"https://api.github.com/repos/dotnet/core-docs/issues{/number}","pulls_url":"https://api.github.com/repos/dotnet/core-docs/pulls{/number}","milestones_url":"https://api.github.com/repos/dotnet/core-docs/milestones{/number}","notifications_url":"https://api.github.com/repos/dotnet/core-docs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dotnet/core-docs/labels{/name}","releases_url":"https://api.github.com/repos/dotnet/core-docs/releases{/id}","deployments_url":"https://api.github.com/repos/dotnet/core-docs/deployments","created_at":"2015-05-19T15:13:32Z","updated_at":"2016-04-05T18:53:01Z","pushed_at":"2016-04-05T19:33:47Z","git_url":"git://github.com/dotnet/core-docs.git","ssh_url":"git@github.com:dotnet/core-docs.git","clone_url":"https://github.com/dotnet/core-docs.git","svn_url":"https://github.com/dotnet/core-docs","homepage":"http://dotnet.github.io/docs/index.html","size":14230,"stargazers_count":67,"watchers_count":67,"language":"CSS","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":78,"mirror_url":null,"open_issues_count":132,"forks":78,"open_issues":132,"watchers":67,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dotnet/core-docs/pulls/381"},"html":{"href":"https://github.com/dotnet/core-docs/pull/381"},"issue":{"href":"https://api.github.com/repos/dotnet/core-docs/issues/381"},"comments":{"href":"https://api.github.com/repos/dotnet/core-docs/issues/381/comments"},"review_comments":{"href":"https://api.github.com/repos/dotnet/core-docs/pulls/381/comments"},"review_comment":{"href":"https://api.github.com/repos/dotnet/core-docs/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/dotnet/core-docs/pulls/381/commits"},"statuses":{"href":"https://api.github.com/repos/dotnet/core-docs/statuses/a3dcc4de576af3ee9c12b6291359d1caab3a4aa5"}}}}