3 ms·
Common, popular, coding styles with a lot of inlined code instead of temporaries will have this issue: public Result MapResult(IntermediateType src) {
by moogly 10y ago
Common, popular, coding styles with a lot of inlined code instead of temporaries will have this issue:
public Result MapResult(IntermediateType src)
{
return new Result
{
Property1 = src.Property1,
Property2 = src.Indirection.Property,
[...]
};
}
or, in C# 6
public Result MapResult(IntermediateType src)
=> new Result
{
Property1 = src.Property1,
Property2 = src.Indirection.Property,
[...]
};
if Indirection is null here (or indeed anything that can throw an NRE within the initializer), the NRE will reference the line starting with return/=>
- kmc059000 10y agoThis is usually where I get NREs too and is the most frustrating. When using object initializers, the line number is the same for the entire initializer, so you don't get much help in determining exactly which line the exception was on. I imagine there is a reason why it's hard, but it seems like it should be easy and incredibly valuable to know which property assignment caused the exception. Regardless, I agree that this is a major source of NREs
- ygra 10y agoGood point, thank you. We're still rather conservative, so except for nameof I haven't yet used any C# 6 features, and the code we write often doesn't look like that, so I tend to forget a few problematic styles sometimes.