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

后端 未结 14 1945
一个人的身影
一个人的身影 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:27

    Using my library The Helper Trinity:

    public void SomeMethod(string firstName, string lastName, int age)
    {
        firstName.AssertNotNull("firstName");
        lastName.AssertNotNull("lastName");
    
        ...
    }
    

    Also supports asserting that enumeration parameters are correct, collections and their contents are non-null, string parameters are non-empty etcetera. See the user documentation here for detailed examples.

提交回复
热议问题