Null parameter checking in C#

前端 未结 9 617
失恋的感觉
失恋的感觉 2021-01-29 21:46

In C#, are there any good reasons (other than a better error message) for adding parameter null checks to every function where null is not a valid value? Obviously, the code tha

9条回答
  •  Happy的楠姐
    2021-01-29 21:59

    int i = Age ?? 0;
    

    So for your example:

    if (age == null || age == 0)
    

    Or:

    if (age.GetValueOrDefault(0) == 0)
    

    Or:

    if ((age ?? 0) == 0)
    

    Or ternary:

    int i = age.HasValue ? age.Value : 0;
    

提交回复
热议问题