Anonymous functions in C#

Posted by Maxim Gershkovich on Stack Overflow See other posts from Stack Overflow or by Maxim Gershkovich
Published on 2011-03-13T16:04:59Z Indexed on 2011/03/13 16:10 UTC
Read the original article Hit count: 233

The following syntax is valid VB.NET code

Dim myCollection As New List(Of Stock)
myCollection.Add(New Stock(Guid.NewGuid, "Item1"))
myCollection.Add(New Stock(Guid.NewGuid, "Item2"))

Dim res As List(Of Stock) = myCollection.FindAll(Function(stock As Stock) As Boolean
                                                     If stock.Description = "Item2" Then
                                                         Return True
                                                     End If
                                                     Return False
                                                 End Function)

How can I accomplish the same thing in C#? I have tried...

myCollection.FindAll(bool delegate(Stock stock) {
                        if (blah blah) {
                        }
                     });

But it appears I have somehow structured it incorrectly as I get the following error. "Error 1 Invalid expression term 'bool'"

© Stack Overflow or respective owner

Related posts about c#

Related posts about vb.net