You could use a generic extension method:
public static int ReturnLength<T>(this T[] arr)
{
if (arr == null)
return 0;
return arr.Length;
}
You can use it with a null reference:
string[] arr = null;
int length = arr.ReturnLength(); // 0
Update: but your non generic extension method should work the same way:
public static int ReturnLength(this Array arr)
{
if (arr == null)
return 0;
return arr.Length;
}
This works because extension methods are just syntactic sugar. Actually they are static methods which you could also use via class name:
int length = MyExtensions.ReturnLength(arr);
In C#, what happens when you call an extension method on a null object?