{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/corefx/issues/3482","labels_url":"https://api.github.com/repos/dotnet/corefx/issues/3482/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/corefx/issues/3482/comments","events_url":"https://api.github.com/repos/dotnet/corefx/issues/3482/events","html_url":"https://github.com/dotnet/corefx/issues/3482","id":108487506,"number":3482,"title":"Proposal: GetValueNoThrow<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)","user":{"login":"Eilon","id":202643,"avatar_url":"https://avatars.githubusercontent.com/u/202643?v=3","gravatar_id":"","url":"https://api.github.com/users/Eilon","html_url":"https://github.com/Eilon","followers_url":"https://api.github.com/users/Eilon/followers","following_url":"https://api.github.com/users/Eilon/following{/other_user}","gists_url":"https://api.github.com/users/Eilon/gists{/gist_id}","starred_url":"https://api.github.com/users/Eilon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Eilon/subscriptions","organizations_url":"https://api.github.com/users/Eilon/orgs","repos_url":"https://api.github.com/users/Eilon/repos","events_url":"https://api.github.com/users/Eilon/events{/privacy}","received_events_url":"https://api.github.com/users/Eilon/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-09-26T19:15:01Z","updated_at":"2015-09-26T21:51:18Z","closed_at":null,"body":"## Scenario\r\n\r\nIn almost every app or library I write I almost always end up needing to write code like this, in at least one place:\r\n\r\n```c#\r\n            SomeClass returnValue;\r\n            return DictionaryOfSomeClass.TryGetValue(someKey, out returnValue) ? returnValue : null;\r\n```\r\n\r\nThe reason that the `IDictionary<TKey, TValue>` contract throws from its indexer for a non-existent key are well-known: an alternative behavior of returning `null` is nonsensical if `TValue` is a value type. Returning `default(TValue)` would work, but would clearly be a misleading behavior.\r\n\r\nBut with dictionaries of class-types it is often desirable and expected to return `null` for a non-existent key.\r\n\r\n## Proposal\r\n\r\nI propose the following extension method (I don't like the name `GetValueNoThrow`, so that can be ignored for now). Please note that it applies only when `TValue` is a class, so it does not violate any behavior for value types.\r\n\r\n```c#\r\n    public static class ClassyDictionaryExtensions\r\n    {\r\n        public static TValue GetValueNoThrow_Ignore_The_Name<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)\r\n            where TValue : class\r\n        {\r\n            if (dictionary == null)\r\n            {\r\n                throw new ArgumentNullException(nameof(dictionary));\r\n            }\r\n            TValue value;\r\n            dictionary.TryGetValue(key, out value);\r\n            return value;\r\n        }\r\n    }\r\n```\r\n\r\nThe new code would end up with one very-easy-to-read line of code:\r\n\r\n```c#\r\n            return DictionaryOfSomeClass.GetValueNoThrow(someKey);\r\n```\r\n\r\n\r\n## Discussion\r\nWould others find such an extension method useful?"},"comment":{"url":"https://api.github.com/repos/dotnet/corefx/issues/comments/143498966","html_url":"https://github.com/dotnet/corefx/issues/3482#issuecomment-143498966","issue_url":"https://api.github.com/repos/dotnet/corefx/issues/3482","id":143498966,"user":{"login":"Eilon","id":202643,"avatar_url":"https://avatars.githubusercontent.com/u/202643?v=3","gravatar_id":"","url":"https://api.github.com/users/Eilon","html_url":"https://github.com/Eilon","followers_url":"https://api.github.com/users/Eilon/followers","following_url":"https://api.github.com/users/Eilon/following{/other_user}","gists_url":"https://api.github.com/users/Eilon/gists{/gist_id}","starred_url":"https://api.github.com/users/Eilon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Eilon/subscriptions","organizations_url":"https://api.github.com/users/Eilon/orgs","repos_url":"https://api.github.com/users/Eilon/repos","events_url":"https://api.github.com/users/Eilon/events{/privacy}","received_events_url":"https://api.github.com/users/Eilon/received_events","type":"User","site_admin":false},"created_at":"2015-09-26T21:51:18Z","updated_at":"2015-09-26T21:51:18Z","body":"@ellismg yeah that would be quite good as well. Definitely more LINQ-y."}}