2

我有一个 VB.NET 函数如下:

Public Function Equilibrium(ParamArray F() As Func(Of Double, Double)) As Boolean

  'I would like to define a function
  ' G(x) = sum of all F(x) 
End Function

该函数的参数是一个函数数组 F(),它接受一个双精度并返回一个双精度。我想在上述函数中将函数 G(x as Double) 定义为 Double 作为所有 F(x) 的总和,但到目前为止我所尝试的方法给了我语法错误。有人可以帮我吗?非常感激。

4

1 回答 1

1

这似乎有效,看看这是不是你的想法......

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim f As New List(Of Func(Of Double, Double))
    f.Add(AddressOf fTest)
    f.Add(AddressOf fTest)
    f.Add(AddressOf fTest)
    Dim b As Boolean = Equilibrium(f.ToArray)
End Sub

Public Function fTest(value As Double) As Double
    Return Math.PI * value
End Function

Public Function Equilibrium(ParamArray F() As Func(Of Double, Double)) As Boolean
    Dim input As Double = 2.38
    Dim G As Func(Of Double, Double) =
        Function(v As Double) As Double
            Return (From fItem As Func(Of Double, Double) In F
                    Select fItem(v)).Sum
        End Function
    Dim sum As Double = G(input)
    ' ...
End Function
于 2014-08-08T10:50:12.223 回答