For example:
<Extension> Public Iterator Function Filter(aBookEnum As IEnumerable(Of Book), selector As Func(Of Book, Boolean)) As IEnumerable(Of Book) For Each thisBook As Book In aBookEnum If selector(thisBook) Then Yield thisBook End If Next End Function
The selector parameter is defined with the Func type. This means that a function must be passed in with a return type of boolean and takes a parameter of type Book.
So when we call the Filter extension method, can do the following:
Let's say you have a function that meets the criteria for the parameter:
Function GetBookOne(myBook As Book) As Boolean Return myBook.Title = "Book One" End Function
You could send this function as a parameter like this:
Dim MySelector As Func(Of Book, Boolean) = AddressOf GetBookOne MyBookShelf.Filter(MySelector)
Or you could use a lambda expression and seriously streamline your code like this:
MyBookShelf.Filter(Function(x) x.Title = "Book One")
So, "x" represents the Book input parameter.
No comments:
Post a Comment