Java:

HashMap<Integer, String> m = new HashMap<>();
m.put(1, "one");

C#:

Dictionary<int, string> d = new Dictionary<int, string>();
d.Add(1, "one");

Okay, fine so far...

Java:

String s = m.get(1);

C#:

String s;
m.TryGetValue(1, out s);

@peterdrake I mean, you could just access it by key like Java as well, but the csharp line is wrapping up a key check and get in one there, so the Java line would need a containsKey around it. Scala would use a getOrElse etc…

Follow

@dazfuller That's the thing -- C#'s Dictionary class doesn't seem to have a Get method, so you have to do this out business.

· · 1 · 0 · 0

@peterdrake it’s just square brackets, so myDict[1] and you’re done

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.