数组的Lambda表达式?

zmb*_*mbq 1 c# lambda binding c#-4.0

在这里阅读这个问题后:在C#中传递一个二维数组的维度,我想知道是否可以使用一些C#4技巧来绑定多维数组的一个索引.希望这样的事情:

var matrix = new int[10, 20];
var row = <some dynamic magic with matrix, 7>;
Run Code Online (Sandbox Code Playgroud)

并且行在语义上与矩阵[7]相同 - 因此行[4]正好是矩阵[7,4].

使用函数很容易:

int some_func(int a, string b);
var some_func_5 = (b=>some_func(5, b));
Run Code Online (Sandbox Code Playgroud)

有人有想法吗?

Jon*_*eet 6

我不认为你可以做任何特别是C#-4特定的事情.我个人会为它构建一个特定的类型:

public sealed class RectangularArrayView<T> : IList<T>
{
    private readonly int row;
    private readonly T[,] array;

    public RectangularArrayView(int row, T[,] array)
    {
        this.row = row;
        this.array = array;
    }

    public T this[int column]
    {
        get { return array[row, column]; }
        set { array[row, column] = value; }
    }

    // etc for other IList<T> methods; use explicit interface implementation
    // for things like Add which don't make sense on arrays
}
Run Code Online (Sandbox Code Playgroud)