C
C#2y ago
Gipper

❔ How to get to one of the arrays in a multidimensional array?

For instance, in:
int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
How do I get to just the
{1, 2}
{1, 2}
part?
13 Replies
sibber
sibber2y ago
so you want to get a row
Gipper
GipperOP2y ago
yeah
sibber
sibber2y ago
it doesnt look like theres a built in way to do that
jcotton42
jcotton422y ago
yeah you can't do that with MD arrays directly you'd have to iterate through it manually using methods like GetLowerBound/GetUpperBound passing the corresponding dimension because it's not an array of arrays it's a truly multidimensional array
Gipper
GipperOP2y ago
So do I have to double loop it and put array4[0,j] into another array?
jcotton42
jcotton422y ago
you can do this directly with an array of arrays (aka jagged arrays) int[][]
Gipper
GipperOP2y ago
oh thanks
jcotton42
jcotton422y ago
MD arrays are really awkward to use much of the time they're a neat idea, but don't work that well in practice
Anton
Anton2y ago
No they just have a bad API
MODiX
MODiX2y ago
Henkypenky#4865
REPL Result: Failure
#nuget CommunityToolkit.HighPerformance

int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

var span2D = new Span2D<int>(array4);

var row = span2D.GetRowSpan(0);

foreach (var x in row)
{
Console.Write($"{x} ");
}
#nuget CommunityToolkit.HighPerformance

int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

var span2D = new Span2D<int>(array4);

var row = span2D.GetRowSpan(0);

foreach (var x in row)
{
Console.Write($"{x} ");
}
Exception: CompilationErrorException
- Field or auto-implemented property cannot be of type 'Span2D<int>' unless it is an instance member of a ref struct.
- Field or auto-implemented property cannot be of type 'Span<int>' unless it is an instance member of a ref struct.
- foreach statement cannot operate on enumerators of type 'Span<int>.Enumerator' in async or iterator methods because 'Span<int>.Enumerator' is a ref struct.
- Field or auto-implemented property cannot be of type 'Span2D<int>' unless it is an instance member of a ref struct.
- Field or auto-implemented property cannot be of type 'Span<int>' unless it is an instance member of a ref struct.
- foreach statement cannot operate on enumerators of type 'Span<int>.Enumerator' in async or iterator methods because 'Span<int>.Enumerator' is a ref struct.
Compile: 1456.056ms | Execution: 0.000ms | React with ❌ to remove this embed.
Henkypenky
Henkypenky2y ago
well it doesn't work here
MODiX
MODiX2y ago
Henkypenky#4865
REPL Result: Success
#nuget CommunityToolkit.HighPerformance

F();

void F()
{
int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
var s = new S();
s.span2D = new Span2D<int>(array4);
s.row = s.span2D.GetRowSpan(0);

foreach (var x in s.row)
{
Console.Write($"{x} ");
}
}

ref struct S
{
public Span2D<int> span2D;
public Span<int> row;

}
#nuget CommunityToolkit.HighPerformance

F();

void F()
{
int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
var s = new S();
s.span2D = new Span2D<int>(array4);
s.row = s.span2D.GetRowSpan(0);

foreach (var x in s.row)
{
Console.Write($"{x} ");
}
}

ref struct S
{
public Span2D<int> span2D;
public Span<int> row;

}
Console Output
1 2
1 2
Compile: 1298.440ms | Execution: 80.430ms | React with ❌ to remove this embed.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server