Using multidimensional and jagged arrays as method parameters in C# can be
challenging for developers.
When these methods are exposed to external users, it requires advanced language knowledge for effective usage.
Determining the appropriate data to pass to these parameters may not be intuitive.
Public Class Program
Public Sub WriteMatrix(matrix As Integer()()) ' Noncompliant: data type is not intuitive
' ...
End Sub
End Class
In this example, it cannot be inferred easily what the matrix should look like. Is it a 2x2 Matrix or even a triangular Matrix?
Using a collection, data structure, or class that provides a more suitable representation of the required data is recommended instead of a
multidimensional array or jagged array to enhance code readability.
Public Class Matrix2x2
' ...
End Class
Public Class Program
Public Sub WriteMatrix(matrix As Matrix2x2) ' Compliant: data type is intuitive
' ...
End Sub
End Class
As a result, avoiding exposing such methods to external users is recommended.
Exceptions
However, using multidimensional and jagged array method parameters internally, such as in private
or internal
methods or
within internal
classes, is compliant since they are not publicly exposed.
Public Class FirstClass
Private Sub UpdateMatrix(matrix As Integer()()) ' Compliant: method is private
' ...
End Sub
End Class
Friend Class SecondClass
Public Sub UpdateMatrix(matrix As Integer()()) ' Compliant: class is internal
' ...
End Sub
End Class