Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use C# generic Dictionary like the Hashtable is used in Java?

Tags:

c#

dictionary

I am following this tutorial and I am working with the Dictionary that I have found out is the equivalent to the Hashtable in Java.

I created my Dictionary like so:

private Dictionary<String, Tile> tiles = new Dictionary<String, Tile>();

Though my dilemma is that when using Dictionary I can not use get, written in Java like so:

Tile tile = tiles.get(x + ":" + y);

How do I accomplish the same thing. Meaning getting x:y as the result?

like image 913
E.A.O.S Avatar asked May 14 '15 20:05

E.A.O.S


People also ask

How do you use C programming?

We can use either a plain text editor (like Notepad) or the IDE's built-in editor. The source code must follow the syntax of the C programming language. After the source file is complete, save it as a *.c file. We'll need a compiler to compile our source code.

Why is C good for beginners?

The programs that you write in C compile and execute much faster than those written in other languages. This is because it does not have garbage collection and other such additional processing overheads. Hence, the language is faster as compared to most other programming languages.

What is C and how it works?

The C programming language works by writing your code into an executable file. The C compiler will take that executable and convert it in its entirety into machine code that will then be executed by your computer at runtime.


1 Answers

Short Answer

Use the indexer or the TryGetValue() method. If the key isn't present, then the former throws a KeyNotFoundException and the latter returns false.

There is really no direct equivalent to the Java Hashtable get() method. That's because Java's get() returns null if the key isn't present.

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

On the other hand, in C# we can map a key to a null value. If either the indexer or the TryGetValue() says that the value associated with a key is null, then that doesn't mean the key isn't mapped. It just means that the key is mapped to null.

Running Example:

using System;
using System.Collections.Generic;

public class Program
{
    private static Dictionary<String, Tile> tiles = new Dictionary<String, Tile>();
    public static void Main()
    {
        // add two items to the dictionary
        tiles.Add("x", new Tile { Name = "y" });
        tiles.Add("x:null", null);

        // indexer access
        var value1 = tiles["x"];
        Console.WriteLine(value1.Name);

        // TryGetValue access
        Tile value2;
        tiles.TryGetValue("x", out value2);
        Console.WriteLine(value2.Name);

        // indexer access of a null value
        var value3 = tiles["x:null"];
        Console.WriteLine(value3 == null);

        // TryGetValue access with a null value
        Tile value4;
        tiles.TryGetValue("x:null", out value4);
        Console.WriteLine(value4 == null);

        // indexer access with the key not present
        try
        {
            var n1 = tiles["nope"];     
        }
        catch(KeyNotFoundException e)
        {
            Console.WriteLine(e.Message);
        }

        // TryGetValue access with the key not present      
        Tile n2;
        var result = tiles.TryGetValue("nope", out n2);
        Console.WriteLine(result);
        Console.WriteLine(n2 == null);
    }

    public class Tile
    {
        public string Name { get; set; }
    }
}
like image 161
Shaun Luttin Avatar answered Nov 14 '22 22:11

Shaun Luttin