How to pass a lambda expression as an argument to a parameter in a method?

3

Is there any way to pass a lambda expression as an argument to a paramer in a method? For example:

Private Sub Metodo(Expressao As ?)
    Lista = Lista.Where(Expressao)
End Sub
    
asked by anonymous 20.12.2016 / 23:48

2 answers

4

The most commonly used way is to use a predefined delegate, such as Func(Of TSource, Boolean) :

Private Sub Metodo(Of TSource)(Expressao As Func(Of TSource, Boolean))
    Lista = Lista.Where(Expressao)
End Sub

TSource is generic, if you know the specific type you can use it. If it is a Integer , it can be:

Private Sub Metodo(Expressao As Func(Of Integer, Boolean))
    Lista = Lista.Where(Expressao)
End Sub

To call is not as convenient as C #:

Metodo(Function(x) x = 0)

Documentation .

    
21.12.2016 / 00:12
1

Code:

Public Class Prj
    Public Sub Metodo(Of T)(Where As Func(Of T, Boolean))
        Lista = Lista.Where(Where)
    End Sub
End Class

How to use?

Dim c As New Prj
c.Metodo(Function(a) a = 1);

References

21.12.2016 / 00:12