Java Map equivalent in C#

前端 未结 3 1515
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 12:29

I\'m trying to hold a list of items in a collection with a key of my choice. In Java, I would simply use Map as follows:

class Test {
  Map         


        
3条回答
  •  被撕碎了的回忆
    2021-01-30 12:54

    You can index Dictionary, you didn't need 'get'.

    Dictionary example = new Dictionary();
    ...
    example.Add("hello","world");
    ...
    Console.Writeline(example["hello"]);
    

    An efficient way to test/get values is TryGetValue (thanx to Earwicker):

    if (otherExample.TryGetValue("key", out value))
    {
        otherExample["key"] = value + 1;
    }
    

    With this method you can fast and exception-less get values (if present).

    Resources:

    Dictionary-Keys

    Try Get Value

提交回复
热议问题