5
Is there any way to pass a lambda expression as a parameter argument in a method? For example:
Private Sub Metodo(Expressao As ?)
Lista = Lista.Where(Expressao)
End Sub
5
Is there any way to pass a lambda expression as a parameter argument in a method? For example:
Private Sub Metodo(Expressao As ?)
Lista = Lista.Where(Expressao)
End Sub
5
The most adopted way is to use a predefined delegate, such as the Func(Of TSource, Boolean)
:
Private Sub Metodo(Of TSource)(Expressao As Func(Of TSource, Boolean))
Lista = Lista.Where(Expressao)
End Sub
The TSource
is generic, if you know the specific type you can use it. If you are a Integer
, 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)
4
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
Browser other questions tagged vb.net lambda-expressions
You are not signed in. Login or sign up in order to post.