I was looking for the best option for handling null objects when calling a method (or chain of methods).
Our usual practice is to check with the condition:
if ( customObject != null ) {
customObject.callMe();
}
What can be improved with extension methods:
Program customObject = null;
if (customObject.NotNull()) {
customObject.CallMe();
}
public static bool NotNull(this object o) {
return o == null;
}
PLEASE PAY ATTENTION: I usually ignore! from my programming practice. Therefore, it is reasonable to say that extension methods are useful to me.
However, it becomes very difficult to work with the goals of the method.
customObject.CallMe().CallMe2() ex...
What do you think, it can be processed in C #, so it CallMeis called only if customObjectit is not null, and it CallMe2is called only if it CallMereturns a non-zero object.
, If . , , vNext, # 5.0, - .