Background: There is this developer principle "Should my function return null or throw an exception if the requested item does not exist?" that I wouldn't like to discuss here. I decided to throw an exception for all cases that have to return a value and this value only wouldn't exist in cases of an (programmatically or logically) invalid request.
And finally my question: Can I mark a function so that the compiler knows that it will never return null and warn anybody who checks if the return value is null?
You can do this using Code Contracts
.
Example :
public String Method1()
{
Contract.Ensures(Contract.Result<String>() != null);
// To do
}