I have properties type int? and decimal? that are being used in a calculation. If the value of any of them is null it has to default to 0. I'm trying to decide between using null-coalescing or GetValueOrDefault() which will also default to 0 if a value is null.
Which approach would be better in terms of readability and performance (if there is any noticeable difference)?
First:
public decimal MyMethod(int memberId)
{
var dto = GetDtoValue(memberId);
return (dto.PropertyOne ?? 0)
+ (dto.PropertyTwo ?? 0)
+ (dto.PropertyThree ?? 0)
- (dto.PropertyFour ?? 0)
+ ...
}
Second:
public decimal MyMethod(int memberId)
{
var dto = GetDtoValue(memberId);
return dto.PropertyOne.GetValueOrDefault())
+ dto.PropertyTwo.GetValueOrDefault())
+ dto.PropertyThree.GetValueOrDefault())
- dto.PropertyFour.GetValueOrDefault())
+ ...
}