In Java it is possible to annotate methods with annotations that allow some processor to infer nullity and give you a warning/compiler error if you'd violate the nullity contract:
@NotNull
public String returnSomething() {
return null; // would give a warning as you violate the contract.
}
// at call site
if (returnSomething() != null) { // would give you a warning about unneccessary if
}
public int doSomethingElse(@NotNull String arg1, @Nullable String arg2) {
return arg1.length() + arg2.length(); // would give you a warning about potential NPE when accessing arg2 without a check
}
// at call site
doSomethingElse(null, null); // would give you a warning about violating contract for arg1
Is there something similar available for C#?