I have an async method with the following signiature:
public async Task UpdateMerchantAttributes(UpdateMerchantRequestModel model).
The model only has one property:
public Dictionary<string, string> Attributes { get; set; }
For a while I was calling it in a test in the following way:
await client.UpdateMerchantAttributes(new UpdateMerchantRequestModel { Attributes =
{
{"businessType", "0"}
}
});
This compiled just fine, but caused a NullReferenceException
at runtime on that line. I was confused about this because client
was not null, and nothing else is referenced in that line (or so it seems at a glance). Then I tried adding an explicit Dictionary declaration, like so:
await client.UpdateMerchantAttributes(new UpdateMerchantRequestModel { Attributes =
new Dictionary<string, string>
{
{"businessType", "0"}
}
});
And now it passes fine. It was my mistake, but this mistake would've cost me much less time if it'd been a compile error rather than a runtime null ref exception. So I'm curious, why does this happen? Does the compiler think that I'm trying to define a dynamic
and that somehow resolves to null
?