Evil use of Maybe monad and extension methods in C#?

前端 未结 8 1842
面向向阳花
面向向阳花 2020-12-07 17:11

edit 2015 This question and its answers are no longer relevant. It was asked before the advent of C# 6, which has the null propagating opertor (?.)

相关标签:
8条回答
  • 2020-12-07 17:37

    In case you're dealing with C# 6.0/VS 2015 and above, they now have a built-in solution for null propagation:

    string ans = nullableString?.Length.ToString(); // null if nullableString == null, otherwise the number of characters as a string.
    
    0 讨论(0)
  • 2020-12-07 17:37

    The IfNotNull solution is the best (until the C# team gives us a null-safe dereferencing operator, that is).

    0 讨论(0)
提交回复
热议问题