<p>I've been doing c# for a long time, and have never come across an easy way to just new up a hash.</p>

<p>I've recently become acquainted with the ruby syntax of hashes and wonder, does anyone know of a simple way to declare a hash as a literal, without doing all the add calls.</p>

<pre><code>{ "whatever" =&gt; {i =&gt; 1}; "and then something else" =&gt; {j =&gt; 2}};
</code></pre>

[{'answer_id': 52443, 'author': 'Wheelie', 'author_id': 1131, 'author_profile': 'https://Stackoverflow.com/users/1131', 'pm_score': 6, 'selected': True, 'text': '<p>If you\'re using C# 3.0 (.NET 3.5) then you can use collection initializers. They\'re not quite as terse as in Ruby but still an improvement.</p>\n\n<p>This example is based on the <a href="http://msdn.microsoft.com/en-us/library/bb531208.aspx" rel="noreferrer">MSDN Example</a></p>\n\n<pre><code>var students = new Dictionary&lt;int, StudentName&gt;()\n{\n    { 111, new StudentName {FirstName="Sachin", LastName="Karnik", ID=211}},\n    { 112, new StudentName {FirstName="Dina", LastName="Salimzianova", ID=317, }},\n    { 113, new StudentName {FirstName="Andy", LastName="Ruth", ID=198, }}\n};\n</code></pre>\n'}, {'answer_id': 52503, 'author': 'Matt', 'author_id': 2338, 'author_profile': 'https://Stackoverflow.com/users/2338', 'pm_score': 3, 'selected': False, 'text': '<p>When I\'m not able to use C# 3.0, I use a helper function that translates a set of parameters into a dictionary.</p>\n\n<pre><code>public IDictionary&lt;KeyType, ValueType&gt; Dict&lt;KeyType, ValueType&gt;(params object[] data)\n{\n    Dictionary&lt;KeyType, ValueType&gt; dict = new Dictionary&lt;KeyType, ValueType&gt;((data == null ? 0 :data.Length / 2));\n    if (data == null || data.Length == 0) return dict;\n\n    KeyType key = default(KeyType);\n    ValueType value = default(ValueType);\n\n    for (int i = 0; i &lt; data.Length; i++)\n    {\n        if (i % 2 == 0)\n            key = (KeyType) data[i];\n        else\n        {\n            value = (ValueType) data[i];\n            dict.Add(key, value);\n        }\n    }\n\n    return dict;\n}\n</code></pre>\n\n<p>Use like this:</p>\n\n<pre><code>IDictionary&lt;string,object&gt; myDictionary = Dict&lt;string,object&gt;(\n    "foo",    50,\n    "bar",    100\n);\n</code></pre>\n'}, {'answer_id': 1863840, 'author': 'Quadriceps Hexa', 'author_id': 226777, 'author_profile': 'https://Stackoverflow.com/users/226777', 'pm_score': -1, 'selected': False, 'text': '<pre><code>using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\n\nnamespace Dictionary\n{\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            Program p = new Program();                \n            Dictionary&lt;object, object &gt; d = p.Dic&lt;object, object&gt;("Age",32,"Height",177,"wrest",36);//(un)comment\n            //Dictionary&lt;object, object&gt; d = p.Dic&lt;object, object&gt;();//(un)comment\n\n            foreach(object o in d)\n            {\n                Console.WriteLine(" {0}",o.ToString());\n            }\n            Console.ReadLine();    \n        }\n\n        public Dictionary&lt;K, V&gt; Dic&lt;K, V&gt;(params object[] data)\n        {               \n            //if (data.Length == 0 || data == null || data.Length % 2 != 0) return null;\n            if (data.Length == 0 || data == null || data.Length % 2 != 0) return new Dictionary&lt;K,V&gt;(1){{ (K)new Object(), (V)new object()}};\n\n            Dictionary&lt;K, V&gt; dc = new Dictionary&lt;K, V&gt;(data.Length / 2);\n            int i = 0;\n            while (i &lt; data.Length)\n            {\n                dc.Add((K)data[i], (V)data[++i]);\n                i++;    \n            }\n            return dc;            \n        }\n    }\n}\n</code></pre>\n'}, {'answer_id': 45072305, 'author': 'Matthew Lock', 'author_id': 74585, 'author_profile': 'https://Stackoverflow.com/users/74585', 'pm_score': 1, 'selected': False, 'text': '<p>Since C# 3.0 (.NET 3.5) hashtable literals can be specified like so:</p>\n\n<pre><code>var ht = new Hashtable {\n    { "whatever", new Hashtable {\n            {"i", 1} \n    } },\n    { "and then something else", new Hashtable { \n            {"j",  2}\n    } }\n};\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/52438', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1220/']