简体   繁体   English

一般地在C#中访问多维数组

[英]Generically accessing multidimensional arrays in C#

C# allows creating and populating multidimensional arrays, here is a simple example: C#允许创建和填充多维数组,这是一个简单的示例:

    public static void Main(String[] args)
    {
        var arr = (int[,])CreateArray(new [] {2, 3}, 8);
        Console.WriteLine("Value: " + arr[0,0]);
    }

    // Creates a multidimensional array with the given dimensions, and assigns the
    // given x to the first array element
    public static Array CreateArray<T>(int[] dimLengths, T x)
    {
        var arr = Array.CreateInstance(typeof(T), dimLengths);
        var indices = new int[dimLengths.Length];
        for (var i = 0; i < indices.Length; i++)
            indices[i] = 0;
        arr.SetValue(x, indices);  // Does boxing/unboxing
        return arr;
    }

This works well. 这很好。 However, for some reason there is no generic version of Array.SetValue(), so the code above does boxing/unboxing, which I'd like to avoid. 但是,由于某种原因,没有通用版本的Array.SetValue(),因此上面的代码可以进行装箱/拆箱,我想避免这种情况。 I was wondering if I missed something or if this is an omission in the .NET API? 我想知道我是否错过了某些内容,或者这是否是.NET API中的遗漏?

No, you are not missing anything: Arrays does not have an option that sets the value without boxing and unboxing. 不,您什么都不会丢失: Arrays没有设置无需装箱和拆箱的值的选项。

You do have an alternative to this with LINQ, but it is probably going to be slower than boxing/unboxing for a single element, because compiling a dynamic lambda would "eat up" the potential benefits: 您确实可以使用LINQ替代它,但是它可能比单个元素的装箱/拆箱要慢,因为编译动态Lambda会“吃掉”潜在的好处:

public static Array CreateArray<T>(int[] dimLengths, T x) {
    var arr = Array.CreateInstance(typeof(T), dimLengths);

    var p = Expression.Parameter(typeof(object), "arr");
    var ind = new Expression[dimLengths.Length];
    for (var i = 0; i < dimLengths.Length; i++) {
        ind[i] = Expression.Constant(0);
    }
    var v = Expression.Variable(arr.GetType(), "cast");
    var block = Expression.Block(
        new[] {v}
    ,   new Expression[] {
            Expression.Assign(v, Expression.Convert(p, arr.GetType()))
        ,   Expression.Assign(Expression.ArrayAccess(v, ind), Expression.Constant(x))
        ,   Expression.Constant(null, typeof(object))
        }
    );
    Expression.Lambda<Func<object, object>>(block, p).Compile()(arr);
    return arr;
}

If you wanted to set all elements in a loop, you could modify the above to compile a dynamically created lambda with multiple nested loops. 如果要在循环中设置所有元素,则可以修改上述内容以编译具有多个嵌套循环的动态创建的lambda。 In this case, you could get an improvement on having to perform multiple boxing and unboxing in a series of nested loops. 在这种情况下,您可以改进必须在一系列嵌套循环中执行多次装箱和拆箱的过程。

for some reason there is no generic version of Array.SetValue() 由于某种原因,没有通用版本的Array.SetValue()

While it is definitely possible to write a generic method similar to SetValue in the Array class, it may not be desirable. 尽管绝对有可能在Array类中编写类似于SetValue的通用方法,但这可能不是理想的。 A generic method on a non-generic class would give a false promise of compile-time type safety, which cannot be guaranteed, because the compiler does not know the runtime type of the Array object. 非泛型类上的泛型方法将错误地保证编译时类型安全,因为编译器不知道Array对象的运行时类型,因此无法保证。

I didn't find any generic ways either to set a value into an Array instance, so I guess the only workaround is to use the unsafe context to avoid boxing. 我也没有找到任何通用方法来将值设置为Array实例,因此我猜唯一的解决方法是使用不安全的上下文来避免装箱。

However, there can be no generic version , now when I think of it. 但是,当我想到它时,就不可能有通用版本 See, when you define a generic method method<T>()... , you do define the parameter for the method: ...<T>(T[] a)... where you have to be specific about the dimensions count, which is one . 请参见,当您定义通用方法method<T>()... ,您确实定义了该方法的参数: ...<T>(T[] a)...在此必须具体说明尺寸计数,即1 To create a twodimensional parameter, you define it like this ...<T>(T[,] a)... and so on. 要创建二维参数,可以像这样定义它...<T>(T[,] a)...依此类推。

As you can see, by the current syntax of C#, you simple cannot create a generic method, which can accept any-dimensional array. 如您所见,使用C#的当前语法,您无法简单地创建可以接受任何维数组的通用方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM