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

时间:2022-02-19 21:33:53

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. I was wondering if I missed something or if this is an omission in the .NET API?

这很好用。但是,由于某些原因,没有通用版本的Array.SetValue(),所以上面的代码执行装箱/拆箱,我想避免。我想知道我是否遗漏了某些内容,或者这是否是.NET API中的遗漏?

2 个解决方案

#1


1  

No, you are not missing anything: Arrays does not have an option that sets the value without boxing and unboxing.

不,你没有遗漏任何东西:数组没有一个选项来设置没有装箱和拆箱的值。

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. In this case, you could get an improvement on having to perform multiple boxing and unboxing in a series of nested loops.

如果要在循环中设置所有元素,可以修改上面的内容以编译具有多个嵌套循环的动态创建的lambda。在这种情况下,您可以在一系列嵌套循环中执行多次装箱和拆箱的改进。

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. 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类中编写类似于SetValue的泛型方法,但可能并不理想。非泛型类的泛型方法会给出编译时类型安全的错误承诺,这是无法保证的,因为编译器不知道Array对象的运行时类型。

#2


0  

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. To create a twodimensional parameter, you define it like this ...<T>(T[,] a)... and so on.

但是,现在当我想到它时,可能没有通用版本。当您定义泛型方法方法 ()...时,请确定方法的参数:... (T [] a)...其中您必须具体说明尺寸计数,即一。要创建一个二维参数,可以像这样定义... (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#的当前语法,您可以简单地创建一个通用方法,它可以接受任何维数组。

#1


1  

No, you are not missing anything: Arrays does not have an option that sets the value without boxing and unboxing.

不,你没有遗漏任何东西:数组没有一个选项来设置没有装箱和拆箱的值。

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. In this case, you could get an improvement on having to perform multiple boxing and unboxing in a series of nested loops.

如果要在循环中设置所有元素,可以修改上面的内容以编译具有多个嵌套循环的动态创建的lambda。在这种情况下,您可以在一系列嵌套循环中执行多次装箱和拆箱的改进。

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. 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类中编写类似于SetValue的泛型方法,但可能并不理想。非泛型类的泛型方法会给出编译时类型安全的错误承诺,这是无法保证的,因为编译器不知道Array对象的运行时类型。

#2


0  

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. To create a twodimensional parameter, you define it like this ...<T>(T[,] a)... and so on.

但是,现在当我想到它时,可能没有通用版本。当您定义泛型方法方法 ()...时,请确定方法的参数:... (T [] a)...其中您必须具体说明尺寸计数,即一。要创建一个二维参数,可以像这样定义... (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#的当前语法,您可以简单地创建一个通用方法,它可以接受任何维数组。