If I want to take a substring I can do something like this:
myString.SubString(0,3);
But now the code needs a null check before I can safely call SubString. So I write a string extension.
public static string SafeSubString(this string input, int length)
{
if (input == null)
return null;
if (input.Length > length)
return input.Substring(0, length);
return input;
}
Now I can write myString.SafeSubString(3);
and avoid null checks.
My question is, is it possible to use a template operator with extensions and somehow circumvent null checks in object methods generically?
eg.
MyObj.AnyMethod()
Maybe you could replace string with the T
Something like in the neighborhood of this
static void Swap<T>(this T myObj, delegate method)
{
if(myObj != null)
method.invoke();
}