4

I have used a MultiDimesional Array as follows

 string[,] Columns = { { "clientA", "clientB" }} if (Columns.Length != 0) { for (int i = 0; i < Columns.Length / 2; i++) { bulkCopy.ColumnMappings.Add(Columns[i, 0], Columns[i, 1]); } } 

After code analysis I got warning message as

Severity Code Description Project File Line Suppression State Warning CA1814 'Order.GetLastOrderID()' uses a multidimensional array of 'string[,]'. Replace it with a jagged array if possible. 

I have researched jagged arrays on internet but How do I replace my array with jagged array.

5
  • can you share GetLastOrderID() code Commented Jan 20, 2017 at 12:37
  • @IkramTurgunbaev actullly the array I used are sql parameters which passed to a method. Commented Jan 20, 2017 at 12:41
  • @IkramTurgunbaev edited some code , please see Commented Jan 20, 2017 at 12:45
  • You should ignore (and suppress) the message if your data is tabular and most cells are filled. Your code is fine. The MSDN page for that warning gives the rationale that it may waste space if the rows have different lengths. But given that strings are reference types, the waste isn't substantial, if there is any at all. Commented Jan 20, 2017 at 12:47
  • @PeterA.Schneider yes, but I was just curious, Thanks Commented Jan 20, 2017 at 12:50

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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. 

Comments