{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/corefx/issues/1942","repository_url":"https://api.github.com/repos/dotnet/corefx","labels_url":"https://api.github.com/repos/dotnet/corefx/issues/1942/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/corefx/issues/1942/comments","events_url":"https://api.github.com/repos/dotnet/corefx/issues/1942/events","html_url":"https://github.com/dotnet/corefx/issues/1942","id":85577695,"number":1942,"title":"Proposal: Dictionary<TKey, TValue>.TryAdd(TKey, TValue)","user":{"login":"stephentoub","id":2642209,"avatar_url":"https://avatars.githubusercontent.com/u/2642209?v=3","gravatar_id":"","url":"https://api.github.com/users/stephentoub","html_url":"https://github.com/stephentoub","followers_url":"https://api.github.com/users/stephentoub/followers","following_url":"https://api.github.com/users/stephentoub/following{/other_user}","gists_url":"https://api.github.com/users/stephentoub/gists{/gist_id}","starred_url":"https://api.github.com/users/stephentoub/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stephentoub/subscriptions","organizations_url":"https://api.github.com/users/stephentoub/orgs","repos_url":"https://api.github.com/users/stephentoub/repos","events_url":"https://api.github.com/users/stephentoub/events{/privacy}","received_events_url":"https://api.github.com/users/stephentoub/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dotnet/corefx/labels/api-approved","name":"api-approved","color":"159818"}],"state":"open","locked":false,"assignee":{"login":"stephentoub","id":2642209,"avatar_url":"https://avatars.githubusercontent.com/u/2642209?v=3","gravatar_id":"","url":"https://api.github.com/users/stephentoub","html_url":"https://github.com/stephentoub","followers_url":"https://api.github.com/users/stephentoub/followers","following_url":"https://api.github.com/users/stephentoub/following{/other_user}","gists_url":"https://api.github.com/users/stephentoub/gists{/gist_id}","starred_url":"https://api.github.com/users/stephentoub/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stephentoub/subscriptions","organizations_url":"https://api.github.com/users/stephentoub/orgs","repos_url":"https://api.github.com/users/stephentoub/repos","events_url":"https://api.github.com/users/stephentoub/events{/privacy}","received_events_url":"https://api.github.com/users/stephentoub/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/dotnet/corefx/milestones/5","html_url":"https://github.com/dotnet/corefx/milestones/Future","labels_url":"https://api.github.com/repos/dotnet/corefx/milestones/5/labels","id":1328080,"number":5,"title":"Future","description":"Milestone tracking the issues triaged for an unspecified future release","creator":{"login":"joshfree","id":1302850,"avatar_url":"https://avatars.githubusercontent.com/u/1302850?v=3","gravatar_id":"","url":"https://api.github.com/users/joshfree","html_url":"https://github.com/joshfree","followers_url":"https://api.github.com/users/joshfree/followers","following_url":"https://api.github.com/users/joshfree/following{/other_user}","gists_url":"https://api.github.com/users/joshfree/gists{/gist_id}","starred_url":"https://api.github.com/users/joshfree/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joshfree/subscriptions","organizations_url":"https://api.github.com/users/joshfree/orgs","repos_url":"https://api.github.com/users/joshfree/repos","events_url":"https://api.github.com/users/joshfree/events{/privacy}","received_events_url":"https://api.github.com/users/joshfree/received_events","type":"User","site_admin":false},"open_issues":560,"closed_issues":145,"state":"open","created_at":"2015-09-29T00:25:45Z","updated_at":"2016-05-04T22:02:20Z","due_on":null,"closed_at":null},"comments":32,"created_at":"2015-06-05T16:42:27Z","updated_at":"2016-05-05T10:09:02Z","closed_at":null,"body":"It's very common to see code that does this:\r\n```C#\r\nDictionary<TKey, TValue> data = ...;\r\n...\r\nif (!data.ContainsKey(key))\r\n    data.Add(key, value);\r\n```\r\nThis forces the dictionary to lookup the key twice when the key isn't yet in the dictionary.\r\n\r\nIt'd be great if we had an additional method on the type:\r\n```C#\r\npublic bool TryAdd(TKey key, TValue value);\r\n```\r\nFunctionally it would behave exactly as if it were implemented like:\r\n```C#\r\npublic bool TryAdd(TKey key, TValue value)\r\n{\r\n    if (ContainsKey(key)) return false;\r\n    Add(key, value);\r\n    return true;\r\n}\r\n```\r\nbut the implementation internally would be done more efficiently, essentially implemented by calling a variant of Insert but that returns false instead of throwing when a duplicate key is found."},"comment":{"url":"https://api.github.com/repos/dotnet/corefx/issues/comments/217117588","html_url":"https://github.com/dotnet/corefx/issues/1942#issuecomment-217117588","issue_url":"https://api.github.com/repos/dotnet/corefx/issues/1942","id":217117588,"user":{"login":"AlainBorletHote","id":19204154,"avatar_url":"https://avatars.githubusercontent.com/u/19204154?v=3","gravatar_id":"","url":"https://api.github.com/users/AlainBorletHote","html_url":"https://github.com/AlainBorletHote","followers_url":"https://api.github.com/users/AlainBorletHote/followers","following_url":"https://api.github.com/users/AlainBorletHote/following{/other_user}","gists_url":"https://api.github.com/users/AlainBorletHote/gists{/gist_id}","starred_url":"https://api.github.com/users/AlainBorletHote/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AlainBorletHote/subscriptions","organizations_url":"https://api.github.com/users/AlainBorletHote/orgs","repos_url":"https://api.github.com/users/AlainBorletHote/repos","events_url":"https://api.github.com/users/AlainBorletHote/events{/privacy}","received_events_url":"https://api.github.com/users/AlainBorletHote/received_events","type":"User","site_admin":false},"created_at":"2016-05-05T10:09:02Z","updated_at":"2016-05-05T10:09:02Z","body":"Please be efficient and implement the 4 methods alltogether:\r\n\r\n- TryAdd\r\n- GetOrAdd\r\n- AddOrUpdate\r\n- TryUpdate\r\n"}}