开发者

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?开发者_高级运维


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.


Why not a set of extension methods?

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜