Span and two dimensional Arrays

前端 未结 4 742
轮回少年
轮回少年 2020-12-19 08:16

Is it possible to use the new System.Memory Span struct with two dimensional arrays of data?

double[,] testMulti = 
    {
        { 1, 2, 3, 4 },
        { 5         


        
4条回答
  •  再見小時候
    2020-12-19 08:45

    Perhaps there's more success to be had working with a jagged array instead of a multidimensional array.

    double[][] testMulti = 
        {
            new double[] { 1, 2, 3, 4 },
            new double[] { 5, 6, 7, 8 },
            new double[] { 9, 9.5f, 10, 11 },
            new double[] { 12, 13, 14.3f, 15 }
        };
    
    Span span = testMulti.AsSpan(2, 1);
    Span slice = span[0].AsSpan(1, 2);
    
    foreach (double d in slice)
        Console.WriteLine(d);
    
    slice[0] = 10.5f;
    
    Console.Write(string.Join(", ", testMulti[2]));
    
    Console.ReadLine();
    

    OUTPUT

    9.5
    10
    9, 10.5, 10, 11
    

提交回复
热议问题