1

I would like to create an extension method for the System.Array class that will take in a char[] or byte[] array and return its Length. I need this method for in case the array is null, and I don't want to litter the code with whole bunch of (arr == null) ? 0 : arr.Length calls.

I want to call it like this: Array.ReturnLength(arr).

I tried implementing it, but I only see it when I do arr.ReturnLength(), but not as an Array method.

public static int ReturnLength(this Array arr)
{
    if (arr == null)
        return 0;
    return arr.Length;
}

Any ideas? Thanks.

Ionian316
  • 2,303
  • 2
  • 28
  • 36
  • 1
    What you want to do is a static extension method. C# doesn't sport those. Static methods require an instance. Instead, since there is no real syntactic difference use your own class and put static methods there. – Benjamin Gruenbaum Mar 14 '14 at 15:53
  • 2
    This is why people use empty arrays to represent "no data" rather than nulls... i.e. `int[] array = new int[0];` rather than `int[] array = null;` – Matthew Watson Mar 14 '14 at 15:58
  • 1
    You: _I only see it when I do `arr.ReturnLength()`,_ What more do you want? It works like an extension method should, then. What is the name of the static class you have put your method into? Your method is a member of that class. Your method is not a member of `System.Array` (even if it "extends" `System.Array`). – Jeppe Stig Nielsen Mar 14 '14 at 15:59
  • 1
    `arr != null ? 0 : arr.Length` ... 0 length means *the array is there and it's empty* while null array means *the array doesn't exist*, it's not the same thing ! Watch out for any implication! – Alex Mar 14 '14 at 16:02
  • Why `arrays`? Isn't it better to use [List](http://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx)?? – huMpty duMpty Mar 14 '14 at 16:02

2 Answers2

3

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?

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Thanks for the link and answer. What would be a reason for choosing the generic over the non-generic method? – Ionian316 Mar 14 '14 at 16:09
  • I think in this case it doesn't matter. You don't need to know what type `T` is since the only information you need is the `Length`. – Tim Schmelter Mar 14 '14 at 16:17
1

As explained in the comments, C# doesn't allow for static extension methods. However, extension methods are just syntactic sugar, and you are allowed to call them as static methods off of the class they are defined in, rather than instance methods off of the object they are defined on.

You already know you can call your method via arr.ReturnLength(). You can also call it via MyExtensionsClass.ReturnLength(arr), where MyExtensionsClass is the static class in which ReturnLength is defined.

goric
  • 11,491
  • 7
  • 53
  • 69
  • Thanks for the quick responses everyone. You're right. All I need is `arr.ReturnLength()`. It'll handle nulls also. Just felt weird calling it if `arr` is null. – Ionian316 Mar 14 '14 at 16:04