Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2" rel="noreferrer"><code>Dictionary</code></a> is probably the closest. <code>System.Collections.Generic.Dictionary</code> implements the <a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.idictionary-2" rel="noreferrer"><code>System.Collections.Generic.IDictionary</code></a> interface (which is similar to Java's <code>Map</code> interface).</p> <p>Some notable differences that you should be aware of:</p> <ul> <li>Adding/Getting items <ul> <li>Java's HashMap has the <code>put</code> and <code>get</code> methods for setting/getting items <ul> <li><code>myMap.put(key, value)</code></li> <li><code>MyObject value = myMap.get(key)</code></li> </ul></li> <li>C#'s Dictionary uses <code>[]</code> indexing for setting/getting items <ul> <li><code>myDictionary[key] = value</code></li> <li><code>MyObject value = myDictionary[key]</code></li> </ul></li> </ul></li> <li><code>null</code> keys <ul> <li>Java's <code>HashMap</code> allows null keys</li> <li>.NET's <code>Dictionary</code> throws an <code>ArgumentNullException</code> if you try to add a null key</li> </ul></li> <li>Adding a duplicate key <ul> <li>Java's <code>HashMap</code> will replace the existing value with the new one.</li> <li>.NET's <code>Dictionary</code> will replace the existing value with the new one if you use <code>[]</code> indexing. If you use the <code>Add</code> method, it will instead throw an <code>ArgumentException</code>.</li> </ul></li> <li>Attempting to get a non-existent key <ul> <li>Java's <code>HashMap</code> will return null.</li> <li>.NET's <code>Dictionary</code> will throw a <code>KeyNotFoundException</code>. You can use the <a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.trygetvalue" rel="noreferrer"><code>TryGetValue</code></a> method instead of the <code>[]</code> indexing to avoid this:<br> <code>MyObject value = null; if (!myDictionary.TryGetValue(key, value)) { /* key doesn't exist */ }</code></li> </ul></li> </ul> <p><code>Dictionary</code>'s has a <a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.containskey" rel="noreferrer"><code>ContainsKey</code></a> method that can help deal with the previous two problems.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload