Alex Hope O'Connor
Alex Hope O'Connor

Reputation: 9684

Most generic array type?

I am looking to create a simple array wrapper with a few helper methods, however I want this wrapper to be able to handle any 1 dimensional array/operation. So I am looking for a base type to cast the internal array to.

Can anyone sugest a type?

Upvotes: 1

Views: 605

Answers (3)

Martin
Martin

Reputation: 12403

It sounds like you want your helper methods to be written as generic extension methods:

public static Array Extensions
{
    public void Foo<T>(this T[] bar)
    {
        //foo this bar
    }
}

Since this is an extension method, all arrays will now have an instance method on them called foo:

int[] a = new int[] { 1, 2, 3 };
a.Foo();

Nb. Rereading your question after seeing the other answer, I see you were asking about making a wrapper class. If you prefer a class, the other answer is technically more correct, but if you're just making a couple of helper methods then extension methods are a far better solution to the problem.

Upvotes: 3

BrokenGlass
BrokenGlass

Reputation: 160882

Why not a set of extension methods?

public static class ArrayExtensions
{
    public static void MyFoo<T>(this T[] sourceArray)
    {
        //...
    }
}

Upvotes: 3

Mark Cidade
Mark Cidade

Reputation: 99957

You can use a generic type parameter:

public class ArrayWrapper<T>
{
   private T[] array;
}

The List<T> class encapsulates an array generically and provides a number of helper methods already.

Upvotes: 3

Related Questions