How can I show that a method will never return null (Design by contract) in C#
问题 I have a method which never returns a null object. I want to make it clear so that users of my API don't have to write code like this: if(Getxyz() != null) { // do stuff } How can I show this intent? 回答1: Unforutnately there is no way built in to C# You can document this fact, but this won't be automatically checked. If you are using resharper, then it can be set up to check this properly when the method is marked with a [NotNull] attribute. Otherwise you can use the Microsoft Contracts