在 C# 8.0 中,为什么我可以在一维数组上使用 range:
var oneDim = new int[5];
var oneDimSlice = oneDim[2..4];
但是不能在多维数组上使用它?
var twoDim = new int[5, 5];
var twoDimSlice = twoDim[2..4, 2..4];
最佳答案
https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/ranges-indexes
Array has more nuanced behavior. Single dimension arrays support both indices and ranges. Multi-dimensional arrays don't. The indexer for a multi-dimensional array has multiple parameters, not a single parameter. Jagged arrays, also referred to as an array of arrays, support both ranges and indexers.
所以答案是否定的,但如果您有兴趣,可以使用锯齿状数组来完成。
关于C# 8.0 对多维数组使用 Range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55065816/