Multidimensional vs Jagged array

前端 未结 2 1657
醉话见心
醉话见心 2021-01-20 02:43

I have used a MultiDimesional Array as follows

  string[,] Columns = { { \"clientA\", \"clientB\" }}

    if (Columns.Length != 0)
            {
                     


        
相关标签:
2条回答
  • 2021-01-20 03:38

    This should work:

     string[][] Columns = { 
         new string[] { "ClientA", "ClientB" } 
     };
    

    If you need to add more elements to the array, it should look like this:

    string[][] Columns = { 
        new string[] { "ClientA", "ClientB" },
        new string[] { "ClientC", "ClientD" },
        new string[] { "ClientE", "ClientF" } 
    };
    

    Then you can access each element with the code below

    Columns[0][0] // Client A
    Columns[0][1] // Client B
    Columns[1][0] // Client C
    Columns[1][1] // Client D
    // etc.
    
    0 讨论(0)
  • 2021-01-20 03:39

    A jagged array is an array of arrays, so you change your definition to:

    string[][] Columns = { new string[] { "clientA", "clientB" }};
    

    Change array access from...

    var value = Columns[0,0];
    

    ...to...

    var value = Columns[0][0];
    

    You also have the option to suppress the warning (right-click on it and select the option you need). According to MSDN, this is a warning that is safe to suppress in certain cases. Check if yours is such a case before you change the code.

    0 讨论(0)
提交回复
热议问题