Dot Net Perls
Top 37 C# Example Pages...

[".syywtyysde.3+CEEST~~}T~~Y666666+BB(CEECCXBP777FXBP777FXCS}T~~}T~~","MultiMap."," A MultiMap has multiple values at a key. With it we add multiple values to a single key in a Dictionary. We see an example implementation of the MultiMap associative array in the C# language. We then use the class.","Example."," The base class library does not provide a MultiMap. This type is a Dictionary with values of type List. Here we show a MultiMap facade class. It uses a string key and any type of value. We add objects at specific keys. ","Note: ","You can find more on classes, and generic classes, on this website as well.","Class ","class","Generic Class ","generic","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","C# program that implements MultiMap","\n\npublic class MultiMap<V>\n{","\n // 1\n ","Dictionary<string, List<V>> _dictionary =\n new Dictionary<string, List<V>>();","\n\n // 2\n ","public void Add(string key, V value)\n {\n List<V> list;\n if (this._dictionary.TryGetValue(key, out list))\n {","\n // 2A.\n ","list.Add(value);\n }\n else\n {","\n // 2B.\n ","list = new List<V>();\n list.Add(value);\n this._dictionary[key] = list;\n }\n }","\n\n // 3\n ","public IEnumerable<string> Keys\n {\n get\n {\n return this._dictionary.Keys;\n }\n }","\n\n // 4\n ","public List<V> this[string key]\n {\n get\n {\n List<V> list;\n if (!this._dictionary.TryGetValue(key, out list))\n {\n list = new List<V>();\n this._dictionary[key] = list;\n }\n return list;\n }\n }\n}","In part 1,"," it uses a Dictionary of string keys. This code always uses string keys, which are the most common. Its goal is to simplify. In part 2, it provides a simple Add method. This method is implemented with TryGetValue. ","TryGetValue ","trygetvalue","ContainsKey ","containskey","To continue,"," we add the value to the List in part 2A. The value is always stored at its key, even with duplicates. In part 3, it exposes Keys. You will want to enumerate over Keys in your code. In part 4, it provides an indexer. ","Tip: ","This provides natural and short syntax for accessing the MultiMap. It returns an empty List if nothing exists.","List ","list","Indexer ","indexer","Note: ","The original implementation of MultiMap here did not add a new list to the Dictionary upon creation in the indexer.","However: ","This reduced the usefulness of the indexer. Artem Korneev wrote in with the improved implementation of get.","Example 2."," Next, this program uses the MultiMap. It demonstrates the Add method, and loops through all the Keys and then the values. The two \"key1\" strings indicate the same value location, so the second value is added to the first in the List. ","Bool ","bool","C# program that uses MultiMap with bools","\n\nclass Program\n{\n static void Main()\n {\n bool b1 = true;\n bool b2 = false;\n bool b3 = false;\n\n MultiMap<bool> m1 = new ","MultiMap<bool>","();\n m1.Add(\"key1\", b1);\n m1.Add(\"key1\", b2);\n m1.Add(\"key2\", b3);\n\n foreach (string k in ","m1.Keys",")\n {\n foreach (bool b in ","m1[k]",")\n {\n Console.WriteLine(k + \"=\" + b);\n }\n }\n Console.ReadLine();\n }\n}\n\n","Output","\n\nkey1=True\nkey1=False\nkey2=False","Example 3."," To continue, this example shows how you could use MultiMap with strings to store names within categories. The strings \"cat\" and \"dog\" will both be stored under \"animal\". This is similar to the previous example. ","Strings ","string","C# program that uses MultiMap with strings","\n\nclass Program\n{\n static void Main()\n {\n const string s1 = \"cat\";\n const string s2 = \"dog\";\n\n MultiMap<string> m1 = new ","MultiMap<string>","();\n m1.Add(\"animal\", s1);\n m1.Add(\"animal\", s2);\n m1.Add(\"human\", \"tom\");\n m1.Add(\"human\", \"tim\");\n m1.Add(\"mineral\", \"calcium\");\n\n foreach (string k in ","m1.Keys",")\n {\n foreach (string v in ","m1[k]",")\n {\n Console.WriteLine(k + \"=\" + v);\n }\n }\n\n Console.ReadLine();\n }\n}\n\n","Output","\n\nanimal=cat\nanimal=dog\nhuman=tom\nhuman=tim\nmineral=calcium","Summary."," We saw an implementation of the MultiMap class in the C# language. My advice is to keep it simple. If you don't need a complex class, it is best to use the minimum that meets your needs. ","Thus: ","The facade here simplifies Dictionary and wraps it with MultiMap features.","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","3679700504","data-ad-format","link","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto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VEX///95cvSaspm/zr6ppPiuqvilofiMhfa6xS0cAAAAfUlEQVR42u3XsQmAMBgF4V9xAAOCfVxAdAEdQtwg1il0fsHS8IpIsLob4OvPnKy1V7WXWR9ER8oMs8hbZ6ImZUYTTTAwMDAwMDAwMDAwMDAfmX4T7Tl3V+o1iYieKifLYs5NFLOYxUQrDAwMDAwMDAwMDAwMzA/MFUSx0GveDtlxrtujxRkAAAAASUVORK5CYII=)"]

["url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAG4AAACCAgMAAADasxWRAAAADFBMVEX/6Nn///+VlZX29va5ksB5AAAASElEQVR4AWPACZj/f0DmjkqOSjKtQgcLyJccZiEE9SOmP8mXHEaBMBoIo4EwGggryJccvvVKKAogTnJUclRyVHLklgmjkqOSAALWyrsxEHLDAAAAAElFTkSuQmCC)","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAABHCAAAAABn1VdPAAADK0lEQVR4Xu3azQ0rKwyA0ZRGExRBEaxdAy24ATbTAAVQAFv2Xvs9KZsQZmy4IN0biW8fKYeM54fMqwn5hyrXZwdyIAdyIP9gB3IgB3IgB3IgB3IgB1J5JdoGqRHs22VciMTDlRjAmncWQixz/IQI/h0gproGKcG82lwcUwRrvrNh1FIj+O8g1j+GOPO6yWTWymDug8x6Bf19WKYgeoHFKJjnArEcoX8OaSvk5VioWCNlC0tV8FJQt0JeoDh0ie7QJTpEDzWHLtEdumQdYirfRs7oOXqcLq8XaB2iH1xoRkK+7/IjXVshr8o3ZTNW4buKH6uuQvQpgUEI8F04CMGtEMN9tQNYwIhgO0nlPuoAgFe6EDoJzUMcYMr/l2Dk2ApPF/IMA1MSny7kBcUp0SG+fIir1Y8t2zrihzG2EMt90DrShzG1EFi6jSernbeKtOqojXuVVv1qJXUFwtUIQ9KvuuM2d/drPa564LbQQNIShKF1KuesKDJBOWclkYlrkNJCijwixG2kDAk0EBJPabAGYdNAsvhNPX/nZWd3ZEnHFq1BoIEk8bKOyu1LFi/r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