编写接受一维或二维数组作为输入的函数 - vb.net

Write a function that accepts either a 1D or a 2D array as input - vb.net

我正在尝试编写一个接受两种不同类型作为参数的函数。这个例子是一个矩阵乘法函数,我希望它适用于一维或二维数组,这样我就可以用它来乘以两个矩阵或乘以一个矩阵和一个向量。这可能吗?

Private Function MatrixMultiplication(byVal input1 As Type1, byVal input2 as Type2)
    If Type1 == Double(,) And Type2 == Double(,) Then
        Do Something
    ElIf Type1 == Double(,) And Type2 == Double() Then
        Do Something
End Function

编辑:有人建议此问题与 that question 重复。我拒绝了这个问题作为我的答案,因为范围不同。这个问题是关于在覆盖时不必要地复制代码。这个问题是关于将不同类型的参数传递给同一个函数,特别是一维数组和二维数组。重载是解决这个问题的方法,但问题完全不同。由于我在不知道“过载”这个词的情况下找不到我正在寻找的答案,所以我相信如果其他人 运行 遇到相同的问题,那么找到这个答案很重要。

结果无论我怎么搜索,我都找不到答案,因为我不知道“过载”这个词。感谢 Jimi 在评论中提供的答案。我重载了该功能并且它有效!这是我的代码,以防万一有人想知道。再次感谢。

    Private Function matrixMultiply(A As Double(,), B As Double(,)) As Double(,)
        If A.GetUpperBound(1) = B.GetUpperBound(0) Then
            ' A(m x n) * B(n x o) = C(m x o)
            Dim C(A.GetUpperBound(0), B.GetUpperBound(1)) As Double
            ' C(i,j) = Sum(k=1 to n)(A(i,k)*B(k,j))
            For i As Integer = 0 To A.GetUpperBound(0)
                For j As Integer = 0 To B.GetUpperBound(1)
                    For k As Integer = 0 To A.GetUpperBound(1)
                        C(i, j) += A(i, k) * B(k, j)
                    Next
                Next
            Next
            Return C
        Else
            Throw New ArgumentException(String.Format("Array dimensions do not match for multiplication"))
        End If
    End Function

    ' overloaded
    Private Function matrixMultiply(A As Double(,), B As Double()) As Double()
        If A.GetUpperBound(1) = B.GetUpperBound(0) Then
            Dim C(A.GetUpperBound(0)) As Double
            For i As Integer = 0 To A.GetUpperBound(0)
                For j = 0 To A.GetUpperBound(1)
                    C(i) += A(i, j) * B(j)
                Next
            Next
            Return C
        Else
            Throw New ArgumentException(String.Format("Array dimensions do not match for multiplication"))
        End If