.NET Dictionary: get or create new

后端 未结 8 1761
感情败类
感情败类 2020-12-08 13:38

I often find myself creating a Dictionary with a non-trivial value class (e.g. List), and then always writing the same code pattern when filling in data.

For example

8条回答
  •  盖世英雄少女心
    2020-12-08 13:44

    And what about this?

    var keyValues = dictionary[key] = dictionary.ContainsKey(key) ? dictionary[key] : new List();
    keyValues.Add(aValueForKey);
    

提交回复
热议问题