How can I check for null without using if
statement, in C#?
public class Bar {
public void foo() {
var discount = _discountService.GetDiscountById(discountId);
//this is anti pattern . is there any other way ?
if (discount == null)
throw new ArgumentException("Discount could not be loaded");
//main logic of methods continues here
}
}
type
but rather that an object has valid state. You can't very well proceed if inputs aren't valid. What's probably more interesting is whyGetDiscountById
can returnnull
in the first place? Simply returndecimal 0