gpt4 book ai didi

C# System.Array 如何计算或查找它有多少维度?

转载 作者:行者123 更新时间:2023-11-30 19:25:44 26 4
gpt4 key购买 nike

如果我有一个未知数组作为 System.Array,我如何才能找到它有多少维度?我在想一些可以称为 Array.GetDimensionCount() 的东西

例如,如果我在 System.Array 类上构建扩展方法并且我想验证 arrayDimensionIndex 不大于或等于数组的维数,我想抛出 ArgumentOutOfRangeException

    /// <summary>
/// Determines whether the value of the specified arrayIndex is within
/// the bounds of this instance for the specified dimension.
/// </summary>
/// <param name="instance">The instance of the array to check.</param>
/// <param name="arrayDimensionIndex">Index of the array dimension.</param>
/// <param name="arrayIndex">The arrayIndex.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentOutOfRangeException">
/// If arrayDimensionIndex exceeds the number of dimesnions in the array.
/// </exception>
public static bool IsInBounds(this Array instance, int arrayDimensionIndex, int arrayIndex)
{
// Determine if this instance has more dimension than the specified array dimension to check.
if(instance.Get?????() <= arrayDimensionIndex)
{
throw new ArgumentOutOfRangeException();
}

// Get the length of the dimension specifed
int arraDimensionLength = instance.GetLength(arrayDimensionIndex);

// Check if the value is withing the array bounds
bool isInBounds = ((uint)arrayIndex < arraDimensionLength);

// Return the result
return isInBounds;
}

谢谢。

最佳答案

您要查找的关键词是Rank .使用 Rank 属性找出数组的维数。

关于C# System.Array 如何计算或查找它有多少维度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27033641/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com