C# rows of multi-dimensional arrays

20,448

Solution 1

You can't pass a row of a rectangular array, you have to use a jagged array (an array of arrays):

int[][] foo = new int[6][];

for(int i = 0; i < 6; i++)
    foo[i] = new int[4];

int[] least = new int[6];

for(int i = 0; i < 6; i++)
    least[i] = FindLeast(foo[i]);

EDIT
If you find it so annoying to use a jagged array and desperately need a rectangular one, a simple trick will save you:

int FindLeast(int[,] rectangularArray, int row)

Solution 2

You don't, with a rectangular array like that. It's a single object.

Instead, you'd need to use a jagged array, like this:

// Note: new int[6][4] will not compile
int[][] foo = new int[6][];
for (int i = 0; i < foo.Length; i++) {
    foo[i] = new int[4];
}

Then you can pass each "sub"-array:

int[] least = new int[foo.Length];
for(int i = 0; i < 6; i++)
{
    least[i] = FindLeast(foo[i]);
}

Note that there's no need to pass foo[i] by reference1, and also it's a good idea to assign local variables values at the point of declaration, when you can. (It makes your code more compact and simpler to understand.)


1 If you're not sure about this, you might want to read my article on parameter passing in C#.

Share:
20,448
CodeKingPlusPlus
Author by

CodeKingPlusPlus

Updated on July 21, 2022

Comments

  • CodeKingPlusPlus
    CodeKingPlusPlus almost 2 years

    In the C# programming language, how do I pass a row of a multi-dimensional array? For example, suppose I have the following:

    int[,] foo;
    foo = new int[6,4];
    int[] least;
    least = new int[6];
    
    for(int i = 0; i < 6; i++)
    {
        least[i] = FindLeast(ref foo[i]);     //How do I pass the ith row of foo???
    }
    

    Also, could anyone explain to me the benefit of having rectangular and jagged arrays in C#? Does this occur in other popular programming languages? (Java?) Thanks for all the help!

  • BlackBear
    BlackBear about 12 years
    You've beaten me by seconds! :P
  • CodeKingPlusPlus
    CodeKingPlusPlus about 12 years
    Can I pass a rectangular array as a jagged array?
  • CodeKingPlusPlus
    CodeKingPlusPlus about 12 years
    Can I pass a rectangular array to a function that accepts jagged arrays?
  • BlackBear
    BlackBear about 12 years
    @CodeKingPlusPlus: No, you can't
  • Niels Abildgaard
    Niels Abildgaard almost 10 years
    So basically this isn't a solution :)
  • Niels Abildgaard
    Niels Abildgaard almost 10 years
    This, to me, seems like a non-solution. What can be done in the case of a rectangular array?
  • Jon Skeet
    Jon Skeet almost 10 years
    @NielsAbildgaard: Nothing, basically. The answer to the question is "you can't do that". It may not be an answer the OP wants to hear, but it's the correct answer.
  • Niels Abildgaard
    Niels Abildgaard almost 10 years
    @JonSkeet That's just not true. It might require more code and be cumbersome (which should be taken into account), but it is definitely possible.
  • Jon Skeet
    Jon Skeet almost 10 years
    @NielsAbildgaard: You can't pass it as an array, which is what the OP appeared to be looking for. You could create your own wrapper type, but there's no indication that that would help the OP. However, if you think you have a better answer, why not add it yourself?
  • Niels Abildgaard
    Niels Abildgaard almost 10 years
    @JonSkeet Just did ;)
  • Jon Skeet
    Jon Skeet almost 10 years
    That's copying the array contents though, which doesn't do the same thing. In particular, if FindLeast modifies the array, then that modification won't be visible in the result. Additionally, the result of foo.Row(i) isn't classified as a variable, so the code won't compile. You simply can't treat a rectangular array as if it were a jagged array - arrays just don't work like that.
  • Niels Abildgaard
    Niels Abildgaard almost 10 years
    I do not understand your comment about compilation. The code runs and compiles.
  • Niels Abildgaard
    Niels Abildgaard almost 10 years
    You are absolutely right! This not passing a reference to a row (which cannot be done). Assuming that FindLeast does not modify the row, this should work. I will rename the method, though :)
  • Jon Skeet
    Jon Skeet almost 10 years
    It may solve the OP's FindLeast example, but it doesn't address the more general question of passing a single row of a rectangular array as a single-dimensional array, which is (as I keep saying) impossible.
  • Niels Abildgaard
    Niels Abildgaard almost 10 years
    It does pass a row. It doesn't pass a reference to the same row. It passes a copy of the row. I think we are in agreement about what C# can and cannot do. I disagree with whether or not reference passing is required for an answer to the original question :)
  • Jon Skeet
    Jon Skeet almost 10 years
    I think that is implicit, to be honest. "Pass a row of a multi-dimensional array" and "pass a copy of a row of a multi-dimensional array" are two different things. I think it's a lot clearer to say "You can't pass a row" than to claim that you can, and actually just pass a copy. I still maintain that the best answer to the OP's question is "you can't".
  • kdbanman
    kdbanman over 8 years
    The "rewritten" code doesn't use your CopyRow method. It actually doesn't appear to be rewritten in any way. Also, the order of strings passed to the ArgumentOutOfRangeException constructor is wrong - the parameter name should come first.
  • Niels Abildgaard
    Niels Abildgaard about 8 years
    That was a simple typo -- (Row -> CopyRow). Fixed.