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?
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.
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.
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.
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.
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; }
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With