IMO, these methods should throw an exception.
A lot of this could (theoretically) be improved in 4.0 using code contracts, as this makes it more formal when the method claims to return null (or not) and requires non-empty (or not).
But no; there is no built-in check for this; the compiler ensures that things are definitely assigned, but not what is assigned to them. In C # 3.0, you could do something sassy, ββlike:
public static T ThrowIfNull<T>(this T obj) where T : class { if(obj == null) throw new SomeException("message"); return obj; }
Then you can use this as a free API:
SomeReturnClass myObj = foo.SomeMethod().ThrowIfNull();
Then myObj will never be null ...
source share