{"componentChunkName":"component---src-pages-markdown-remark-fields-slug-js","path":"/engineering/how-to-create-and-use-dictionary-csharp/","result":{"data":{"markdownRemark":{"id":"9335814f-04d3-5bc1-981f-cda1772f1001","excerpt":"Introduction Dictionaries Dictionary<TKey, TValue> is used for storing a key-value pair. It is a generic collection that is defined in the System.Collection…","html":"<h2 id=\"introduction\" style=\"position:relative;\"><a href=\"#introduction\" aria-label=\"introduction permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Introduction</h2>\n<p>Dictionaries <code>Dictionary&#x3C;TKey, TValue></code> is used for storing a key-value pair. It is a generic collection that is defined in the <em>System.Collection.Generic</em> namespace and implements the <code>IDictionary&#x3C;TKey, TValue></code> interface.</p>\n<h2 id=\"create-a-dictionary\" style=\"position:relative;\"><a href=\"#create-a-dictionary\" aria-label=\"create a dictionary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Create a dictionary</h2>\n<p>Suppose we want to store the scores of a player in a cricket match, Then we have to save the key as the name of the player and score as value.</p>\n<p>To create a dictionary of key and value type as a string and int, respectively, we just simply need to initialize as below.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"c#\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk10\">Dictionary</span><span class=\"mtk1\">&lt;</span><span class=\"mtk4\">string</span><span class=\"mtk1\">, </span><span class=\"mtk4\">int</span><span class=\"mtk1\">&gt; </span><span class=\"mtk12\">dict</span><span class=\"mtk1\"> = </span><span class=\"mtk4\">new</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Dictionary</span><span class=\"mtk1\">&lt;</span><span class=\"mtk4\">string</span><span class=\"mtk1\">, </span><span class=\"mtk4\">int</span><span class=\"mtk1\">&gt;();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk10\">Dictionary</span><span class=\"mtk1\">&lt;</span><span class=\"mtk4\">string</span><span class=\"mtk1\">, </span><span class=\"mtk4\">int</span><span class=\"mtk1\">&gt; </span><span class=\"mtk12\">dict1</span><span class=\"mtk1\"> = </span><span class=\"mtk4\">new</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Dictionary</span><span class=\"mtk1\">&lt;</span><span class=\"mtk4\">string</span><span class=\"mtk1\">, </span><span class=\"mtk4\">int</span><span class=\"mtk1\">&gt;(</span><span class=\"mtk7\">5</span><span class=\"mtk1\">); </span><span class=\"mtk3\">// Creates a dictionary with default initial capacity of 5</span></span></code></pre>\n<p>The first line will create a dictionary with string type of key and int type of value and will have the initial default capacity as well default equality comparer for the key type.</p>\n<p>The Second line will create the same dictionary as line 1 except for the initial default capacity. It will create a dictionary with an initial default capacity of 5.</p>\n<p>We can create the dictionary by using the different constructors of the dictionary. Here I have talked about the two constructors only.</p>\n<h2 id=\"add-elements\" style=\"position:relative;\"><a href=\"#add-elements\" aria-label=\"add elements permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Add elements</h2>\n<p>We can add the elements in the dictionary by using add method of it.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"c#\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">dict</span><span class=\"mtk1\">.</span><span class=\"mtk11\">Add</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;Player&quot;</span><span class=\"mtk1\">,</span><span class=\"mtk7\">42</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">dict</span><span class=\"mtk1\">.</span><span class=\"mtk11\">Add</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;Player1&quot;</span><span class=\"mtk1\">,</span><span class=\"mtk7\">38</span><span class=\"mtk1\">);</span></span></code></pre>\n<p>It will add the elements in the dictionary. </p>\n<p><strong>Note:</strong> <em>Keys</em> can not be <code>null</code> or <code>duplicate</code>. If we add the duplicate or null as a key, then the application will throw the run time exception, but <em>Values</em> can be <code>null</code> or <code>duplicate</code>.</p>\n<h2 id=\"traverse\" style=\"position:relative;\"><a href=\"#traverse\" aria-label=\"traverse permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Traverse</h2>\n<p>To traverse through a dictionary, we can use a foreach loop or a for a loop.</p>\n<h3 id=\"foreach-loop\" style=\"position:relative;\"><a href=\"#foreach-loop\" aria-label=\"foreach loop permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>foreach loop</h3>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"c#\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">foreach</span><span class=\"mtk1\"> (</span><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">item</span><span class=\"mtk1\"> </span><span class=\"mtk15\">in</span><span class=\"mtk1\"> </span><span class=\"mtk12\">dict</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">{</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">Console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">WriteLine</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;Key &quot;</span><span class=\"mtk1\">+ </span><span class=\"mtk12\">item</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Key</span><span class=\"mtk1\"> +</span><span class=\"mtk8\">&quot; Value &quot;</span><span class=\"mtk1\">+ </span><span class=\"mtk12\">item</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Value</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<h3 id=\"for-loop\" style=\"position:relative;\"><a href=\"#for-loop\" aria-label=\"for loop permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>for loop</h3>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"c#\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">for</span><span class=\"mtk1\"> (</span><span class=\"mtk4\">int</span><span class=\"mtk1\"> </span><span class=\"mtk12\">i</span><span class=\"mtk1\"> = </span><span class=\"mtk7\">0</span><span class=\"mtk1\">; </span><span class=\"mtk12\">i</span><span class=\"mtk1\"> &lt; </span><span class=\"mtk12\">dict</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Count</span><span class=\"mtk1\">; </span><span class=\"mtk12\">i</span><span class=\"mtk1\">++)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">{</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">     </span><span class=\"mtk12\">Console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">WriteLine</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;Key &quot;</span><span class=\"mtk1\"> + </span><span class=\"mtk12\">dict</span><span class=\"mtk1\">.</span><span class=\"mtk11\">ElementAt</span><span class=\"mtk1\">(</span><span class=\"mtk12\">i</span><span class=\"mtk1\">).</span><span class=\"mtk12\">Key</span><span class=\"mtk1\"> + </span><span class=\"mtk8\">&quot; Value &quot;</span><span class=\"mtk1\"> + </span><span class=\"mtk12\">dict</span><span class=\"mtk1\">.</span><span class=\"mtk11\">ElementAt</span><span class=\"mtk1\">(</span><span class=\"mtk12\">i</span><span class=\"mtk1\">).</span><span class=\"mtk12\">Value</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<p>In the above code, we are using the </p>\n<ol>\n<li><strong>Key</strong> Property of dictionary element which will return the Key of the dictionary element.</li>\n<li><strong>Value</strong> Property of dictionary element, which will return the value of the dictionary element.</li>\n<li><strong>Count</strong> Property which will return the number of elements in the dictionary.</li>\n<li><strong>ElementAt(int index)</strong> method of a dictionary, which will return the element at the specified index.</li>\n</ol>\n<h2 id=\"getupdate-specified-keys-value\" style=\"position:relative;\"><a href=\"#getupdate-specified-keys-value\" aria-label=\"getupdate specified keys value permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Get/Update specified key's value</h2>\n<p>To get the specified key's value from the dictionary, we can directly use the key as an index. </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"c#\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">Console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">WriteLine</span><span class=\"mtk1\">(</span><span class=\"mtk12\">dict</span><span class=\"mtk1\">[</span><span class=\"mtk8\">&quot;Player&quot;</span><span class=\"mtk1\">]); </span><span class=\"mtk3\">// Prints 42</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">Console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">WriteLine</span><span class=\"mtk1\">(</span><span class=\"mtk12\">dict</span><span class=\"mtk1\">[</span><span class=\"mtk8\">&quot;Player1&quot;</span><span class=\"mtk1\">]); </span><span class=\"mtk3\">// Prints 38</span></span></code></pre>\n<p>To update the specified key's value of the dictionary, we can set it as below.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"c#\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">dict</span><span class=\"mtk1\">[</span><span class=\"mtk8\">&quot;Player&quot;</span><span class=\"mtk1\">]=</span><span class=\"mtk7\">45</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">Console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">WriteLine</span><span class=\"mtk1\">(</span><span class=\"mtk12\">dict</span><span class=\"mtk1\">[</span><span class=\"mtk8\">&quot;Player1&quot;</span><span class=\"mtk1\">]); </span><span class=\"mtk3\">// Prints 45</span></span></code></pre>\n<p><strong>Note:</strong> </p>\n<ol>\n<li>It will give the run time exception if the given key is not present in the dictionary, and we try to access the value of that key so we can first check for the key using the <strong>ContainsKey(TKey)</strong> method of the dictionary which will return true/false if the key is present or not in the dictionary.</li>\n<li>Suppose if we want to set the value of a key that is not present in the dictionary, then it will automatically add that element into the dictionary and sets the value of it.</li>\n</ol>\n<h2 id=\"remove\" style=\"position:relative;\"><a href=\"#remove\" aria-label=\"remove permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Remove</h2>\n<p>To remove elements from a dictionary, we can use the below methods.</p>\n<h3 id=\"remove-specific-key\" style=\"position:relative;\"><a href=\"#remove-specific-key\" aria-label=\"remove specific key permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Remove specific key</h3>\n<p>To remove a specific key, we can use the <strong>Remove(TKey)</strong> method of the dictionary.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"c#\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">dict</span><span class=\"mtk1\">.</span><span class=\"mtk11\">Remove</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;Player1&quot;</span><span class=\"mtk1\">); </span><span class=\"mtk3\">// Removes Player1 from dictionary</span></span></code></pre>\n<h3 id=\"clear-dictionary\" style=\"position:relative;\"><a href=\"#clear-dictionary\" aria-label=\"clear dictionary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Clear dictionary</h3>\n<p>To clear all the elements of the dictionary, we can use the <strong>Clear()</strong> method of the dictionary.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"c#\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">dict</span><span class=\"mtk1\">.</span><span class=\"mtk11\">Clear</span><span class=\"mtk1\">(); </span><span class=\"mtk3\">// Clears all the elements and count will be 0</span></span></code></pre>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>In this article, We have learned about some of the basics of dictionaries. Dictionaries help to use to manage data in Key-Value pair, which we can easily access and manipulate the data accordingly.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n</style>","headings":[{"value":"Introduction","depth":2},{"value":"Create a dictionary","depth":2},{"value":"Add elements","depth":2},{"value":"Traverse","depth":2},{"value":"foreach loop","depth":3},{"value":"for loop","depth":3},{"value":"Get/Update specified key's value","depth":2},{"value":"Remove","depth":2},{"value":"Remove specific key","depth":3},{"value":"Clear dictionary","depth":3},{"value":"Conclusion","depth":2}],"fields":{"slug":"/engineering/how-to-create-and-use-dictionary-csharp/"},"frontmatter":{"metatitle":null,"metadescription":null,"description":"In this article, we will talk about how to create and use the Dictionary in C#.","title":"How to create and use the Dictionary in C#","canonical":null,"date":"May 27, 2021","updated_date":null,"tags":["C#","Dictionary"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/70d1172c5171ae195aeb525bdf6086b3/03979/coverimage.png","srcSet":"/static/70d1172c5171ae195aeb525bdf6086b3/f5f11/coverimage.png 200w,\n/static/70d1172c5171ae195aeb525bdf6086b3/6d133/coverimage.png 400w,\n/static/70d1172c5171ae195aeb525bdf6086b3/03979/coverimage.png 800w,\n/static/70d1172c5171ae195aeb525bdf6086b3/aca38/coverimage.png 1200w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Hemant Manwani","github":"hemant404","bio":"Hemant Manwani is a Software Engineer at LoginRadius who is interested in how the web works and developer tools that make working with web easier.","avatar":null}}}},"pageContext":{"id":"9335814f-04d3-5bc1-981f-cda1772f1001","fields__slug":"/engineering/how-to-create-and-use-dictionary-csharp/","__params":{"fields__slug":"engineering"}}},"staticQueryHashes":["1171199041","1384082988","1711371485","1753898100","2100481360","229320306","23180105","528864852"]}