Below is a little Dictionary extension method for mimicking upsert functionality on in-memory
System.Collections.Generic.
1: public static class DictionaryExtensions
2: {
3: public static TValue Upsert<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
4: {
5: if (dictionary.ContainsKey(key))
6: {
7: // Update value to existing key
8: dictionary[key] = value;
9: }
10: else
11: {
12: // Insert new key
13: dictionary.Add(key, value);
14: }
15: return value;
16: }
17: }
Disclaimer: There are several instances where it is inappropriate to leverage upsert-like functionality for your in-memory hashtables. As with any particular code abstraction, the consumer will need to determine when it is fair to use the syntax shortcut.
As a short sidebar, if you use Code Snippet plugin for Live Writer, you’ll have to run as administrator as it operates on:
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.mshtml, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.mshtml, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at WLWPluginBase.Win32.Win32IEHelper.GetSelectedText(IntPtr handle)
at CodeSnippet.CodeSnippetPlugin.CreateContent(IWin32Window dialogOwner, String& content)
at WindowsLive.Writer.PostEditor.ContentSources.ContentSourceManager.PerformInsertion(IContentSourceSite sourceSite, ContentSourceInfo contentSource)