c# - How do you declare a Predicate Delegate inline? -


i'm using c#.

so have object has fields, doesn't matter what. have generic list of these objects.

list<myobject> myobjects = new list<myobject>(); myobjects.add(myobject1); myobjects.add(myobject2); myobjects.add(myobject3); 

so want remove objects list based on criteria. instance, myobject.x >= 10. use removeall(predicate<t> match) method this.

i know can define delegate can passed removeall, know how define inline anonymous delegate, instead of creating bunch of delegate functions used in once place.

there's 2 options, explicit delegate or delegate disguised lamba construct:

explicit delegate

myobjects.removeall(delegate (myobject m) { return m.x >= 10; }); 

lambda

myobjects.removeall(m => m.x >= 10); 

addition:

performance wise both equal. matter of fact, both language constructs generate same il when compiled. because c# 3.0 extension on c# 2.0, compiles c# 2.0 constructs :)


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -