.net 如何检查数组上的扩展方法是否具有特定的大小?

.net 如何检查数组上的扩展方法是否具有特定的大小?,.net,vb.net,.net,Vb.net,我正在使用基于数组的扩展方法,我想知道是否有一种简单的方法来检查数组是否具有指定的大小,而不是我执行数组的复制粘贴 if array.count != 1000 throw new exception "size of the array does not match" 大约50分钟 这是我使用的扩展的一个小样本,我得到了更多 <Extension()> Public Function IsWhite(ByVal board() As bitPiece, ByVa

我正在使用基于数组的扩展方法,我想知道是否有一种简单的方法来检查数组是否具有指定的大小,而不是我执行数组的复制粘贴

   if array.count != 1000
      throw new exception "size of the array does not match"
大约50分钟

这是我使用的扩展的一个小样本,我得到了更多

<Extension()>
Public Function IsWhite(ByVal board() As bitPiece, ByVal pos As Integer) As Boolean
    Return (board(pos) And bitPiece.White) = bitPiece.White
End Function

<Extension()>
Public Function IsBlack(ByVal board() As bitPiece, ByVal pos As Integer) As Boolean
    Return (board(pos) And bitPiece.Black) = bitPiece.Black
End Function

<Extension()>
Public Function IsRook(ByVal board() As bitPiece, ByVal pos As Integer) As Boolean
    Return (board(pos) And bitPiece.Rook) = bitPiece.Rook
End Function

<Extension()>
Public Function IsBishop(ByVal board() As bitPiece, ByVal pos As Integer) As Boolean
    Return (board(pos) And bitPiece.Bishop) = bitPiece.Bishop
End Function

<Extension()>
Public Function IsKnight(ByVal board() As bitPiece, ByVal pos As Integer) As Boolean
    Return (board(pos) And bitPiece.knight) = bitPiece.knight
End Function

<Extension()>
Public Function IsQueen(ByVal board() As bitPiece, ByVal pos As Integer) As Boolean
    Return (board(pos) And bitPiece.Queen) = bitPiece.Queen
End Function

<Extension()>
Public Function IsKing(ByVal board() As bitPiece, ByVal pos As Integer) As Boolean
    Return (board(pos) And bitPiece.King) = bitPiece.King
End Function

<Extension()>
Public Function IsPawn(ByVal board() As bitPiece, ByVal pos As Integer) As Boolean
    Return (board(pos) And bitPiece.Pawn) = bitPiece.Pawn
End Function

公共函数IsWhite(ByVal board()作为位片,ByVal pos作为整数)作为布尔值
返回(板(位置)和位片.白色)=位片.白色
端函数
公共函数IsBlack(ByVal board()作为位片,ByVal pos作为整数)作为布尔值
返回(板(位置)和位片.黑色)=位片.黑色
端函数
公共函数IsRook(ByVal board()作为位片,ByVal pos作为整数)作为布尔值
返回(板(位置)和比特片.Rook)=比特片.Rook
端函数
公共函数IsBishop(ByVal board()作为位片,ByVal pos作为整数)作为布尔值
返回(板(位置)和位片。Bishop)=位片。Bishop
端函数
公共函数IsKnight(ByVal board()作为位片,ByVal pos作为整数)作为布尔值
返回(棋盘(位置)和比特币.knight)=比特币.knight
端函数
公共函数IsQueen(ByVal board()作为位片,ByVal pos作为整数)作为布尔值
返回(板(位置)和位片.皇后)=位片.皇后
端函数
公共函数IsKing(ByVal board()作为位片,ByVal pos作为整数)作为布尔值
返回(板(位置)和比特片.国王)=比特片.国王
端函数
公共函数IsPawn(ByVal board()作为位片,ByVal pos作为整数)作为布尔值
返回(棋盘(位置)和棋子。棋子)=棋子。棋子
端函数

看来您需要的是:

public static void AssertLength<T>(this T[] arr, int expectedLength)
{
    if (arr == null || arr.Length != expectedLength)
    {
        throw new Exception("Size of the array does not match");
    }
}
public static void CheckArrLength(this double[] x, int length)
    {
        if (x.Length != length)
            throw new ArgumentException("Invalid Array Size."); 
    }

由于您的数组不是真正的数组,而是表示特定的数据结构(棋盘),您是否考虑过为其创建专用类型

class Board
{
    private readonly bitPiece[] board;

    public Board()
    {
        board = new bitPiece[64];
    }

    Public Function IsBlack(ByVal pos As Integer) As Boolean
        Return (board(pos) And bitPiece.Black) = bitPiece.Black
    End Function
}

您正在寻找以下内容:

public static void AssertLength<T>(this T[] arr, int expectedLength)
{
    if (arr == null || arr.Length != expectedLength)
    {
        throw new Exception("Size of the array does not match");
    }
}
public static void CheckArrLength(this double[] x, int length)
    {
        if (x.Length != length)
            throw new ArgumentException("Invalid Array Size."); 
    }
每个方法都可以这样调用它:

public static void Func(this double[] x)
    {
        x.CheckArrLength(1000);
        ...
    }

“扩展”是什么意思?好的,然后用“扩展方法”这个术语。我还不清楚你在找什么。如果要重用逻辑,请在公共静态方法中抛出这两行代码。我遗漏了什么吗?文本/标题用“扩展方法”修复,问题是我想要一种“通用”的方法来检查数组,而不必进行大量的复制/粘贴。您认为您可以发布两种扩展方法,以便我们可以在更现实的环境中讨论这一点吗?(我假设你没有实际检查
1000
ArgumentException
更合适。@Tim-是的,更具体的异常类型会更好,我只是基于问题中的示例。有没有简单的方法附加它来对数组进行验证检查,看看我更新的问题itHa,用C#/VB mashup做了一个很好的手术但是答案很好。嗯,我可以试着看看我是否可以做这样的事情,在我的情况下,这将更像是一个部分类板:-pbut,是否可以对扩展进行“通用验证”检查?对未来的了解可能会很好use@Fredou:如果要在每个方法中检查某些内容,则无法在每个方法中插入至少一行代码。您可以通过确保阵列在创建时具有正确的大小,并防止传入其他阵列来避免此问题。我希望不必这样做就可以做到这一点,因为在查看它时,我还需要检查pos,它需要在>=0和