What is the smoothest, most appealing syntax you've found for asserting parameter correctness in c#?

后端 未结 14 1922
一个人的身影
一个人的身影 2020-12-22 23:50

A common problem in any language is to assert that parameters sent in to a method meet your requirements, and if they don\'t, to send nice, informative error messages. This

14条回答
  •  旧巷少年郎
    2020-12-23 00:47

    My preference would be to just evaluate the condition and pass the result rather than passing an expression to be evaluated and the parameter on which to evaluate it. Also, I prefer to have the ability to customize the entire message. Note that these are simply preferences -- I'm not saying that your sample is wrong -- but there are some cases where this is very useful.

    Helper.Validate( firstName != null || !string.IsNullOrEmpty(directoryID),
                     "The value for firstName cannot be null if a directory ID is not supplied." );
    

提交回复
热议问题