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 comparing apples and oranges here… the analog to the java one would be

m[1]

What you picked is the try..get pattern used e.g. like this:

if (m.TryGetValue(1, out var val)) return val;
else return „fallback“, or, shorter

return m.TryGetValue(1, out var val) ? val : „fallback“;

@flq Yeah, I was just looking in the API for Dictionary and wondering why, if there was an Add method, there was no Get method. I had forgotten about square bracket access.

@peterdrake @flq why not edit your original post then? Or delete it, since it's now rendered pointless?

Follow

@damselfly @flq My server is running variant software that doesn't allow editing posts. I might delete it, but could someone else who overlooked the same thing benefit from the replies?

Sign in to participate in the conversation
CleverLibre Social

CleverLibre Social is an inclusive social instance for open discussion, learning, and community.
All cultures welcome.
Hate speech and harassment strictly forbidden.