c#未设置字典对象引用

c#未设置字典对象引用,c#,dictionary,C#,Dictionary,为什么这段代码在第一条if语句上返回nullreferenceexoption?如何补救 void Main() { addOrIncrement(new KeyValuePair<string,long>("1",1)); addOrIncrement(new KeyValuePair<string,long>("1",1)); } public Dictionary<string, long> Result { get; set; }

为什么这段代码在第一条if语句上返回
nullreferenceexoption
?如何补救

void Main()
{
    addOrIncrement(new KeyValuePair<string,long>("1",1));
    addOrIncrement(new KeyValuePair<string,long>("1",1));
}

public Dictionary<string, long> Result { get; set; }

public void addOrIncrement(KeyValuePair<string,long> pair){
    if(Result.ContainsKey(pair.Key))
    {
        Result[pair.Key] += pair.Value;
    } else {
        Result.Add(pair.Key, pair.Value);
    }
}
void Main()
{
addOrIncrement(新的KeyValuePair(“1”,1));
addOrIncrement(新的KeyValuePair(“1”,1));
}
公共字典结果{get;set;}
public void addOrIncrement(键值对){
if(Result.ContainsKey(pair.Key))
{
结果[pair.Key]+=pair.Value;
}否则{
结果.添加(pair.Key,pair.Value);
}
}

Result
从未初始化,因此它是
null

public Dictionary<string, long> Result { get; set; } = new Dictionary<string, long>();
公共字典结果{get;set;}=new Dictionary();

我今天脑子不好使

结果
尚未初始化


您需要添加
Result=newdictionary()第一个

哇,我的头今天显然不工作了。我怎么会错过呢?