<p>So I have an object which has some fields, doesn't really matter what.
I have a generic list of these objects.</p>
<pre><code>List&lt;MyObject&gt; myObjects = new List&lt;MyObject&gt;();
myObjects.Add(myObject1);
myObjects.Add(myObject2);
myObjects.Add(myObject3);
</code></pre>
<p>So I want to remove objects from my list based on some criteria.
For instance, <code>myObject.X &gt;= 10.</code>
I would like to use the <code>RemoveAll(Predicate&lt;T&gt; match)</code> method for to do this.</p>
<p>I know I can define a delegate which can be passed into RemoveAll, but I would like to know how to define this inline with an anonymous delegate, instead of creating a bunch of delegate functions which are only used in once place.</p>

[{'answer_id': 59172, 'author': 'Erik van Brakel', 'author_id': 909, 'author_profile': 'https://Stackoverflow.com/users/909', 'pm_score': 7, 'selected': True, 'text': "<p>There's two options, an explicit delegate or a delegate disguised as a lamba construct:</p>\n<p>explicit delegate</p>\n<pre><code>myObjects.RemoveAll(delegate (MyObject m) { return m.X &gt;= 10; });\n</code></pre>\n<p>lambda</p>\n<pre><code>myObjects.RemoveAll(m =&gt; m.X &gt;= 10);\n</code></pre>\n<hr />\n<p>Performance wise both are equal. As a matter of fact, both language constructs generate the same IL when compiled. This is because C# 3.0 is basically an extension on C# 2.0, so it compiles to C# 2.0 constructs</p>\n"}, {'answer_id': 59174, 'author': 'Mark Brackett', 'author_id': 2199, 'author_profile': 'https://Stackoverflow.com/users/2199', 'pm_score': 4, 'selected': False, 'text': "<p>The lambda C# 3.0 way:</p>\n\n<pre><code>myObjects.RemoveAll(m =&gt; m.x &gt;= 10);\n</code></pre>\n\n<p>The anonymous delegate C# 2.0 way:</p>\n\n<pre><code>myObjects.RemoveAll(delegate (MyObject m) {\n   return m.x &gt;= 10;\n});\n</code></pre>\n\n<p>And, for the VB guys, the VB 9.0 lambda way:</p>\n\n<pre><code>myObjects.RemoveAll(Function(m) m.x &gt;= 10)\n</code></pre>\n\n<p>Unfortunately, VB doesn't support an anonymous delegate.</p>\n"}, {'answer_id': 59177, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 4, 'selected': False, 'text': '<pre><code>  //C# 2.0\n  RemoveAll(delegate(Foo o){ return o.X &gt;= 10; });\n</code></pre>\n\n<p>or</p>\n\n<pre><code>  //C# 3.0\n  RemoveAll(o =&gt; o.X &gt;= 10);\n</code></pre>\n\n<p>or</p>\n\n<pre><code>  Predicate&lt;Foo&gt; matches = delegate(Foo o){ return o.X &gt;= 10; });\n  //or Predicate&lt;Foo&gt; matches = o =&gt; o.X &gt;= 10;\n  RemoveAll(matches);\n</code></pre>\n'}, {'answer_id': 47375389, 'author': 'Nayas Subramanian', 'author_id': 4315441, 'author_profile': 'https://Stackoverflow.com/users/4315441', 'pm_score': 1, 'selected': False, 'text': '<p>Predicate is a delegate which takes an param and returns a boolean.</p>\n\n<p>We can do the same in following ways</p>\n\n<p>1) <strong>Using inline Lambda expression</strong></p>\n\n<pre><code>RemoveAll(p=&gt; p.x &gt; 2);\n</code></pre>\n\n<p>2) <strong>Using anonymous function</strong></p>\n\n<pre><code>RemoveAll(delegate(myObject obj){\n\n  return obj.x &gt;=10;\n})\n</code></pre>\n\n<p>3) <strong>Using Predicate delegate</strong></p>\n\n<pre><code>Predicate&lt;myObject&gt; matches = new Predicate&lt;myObject&gt;(IsEmployeeIsValid);\nRemoveAll(matches);\n\nPredicate&lt;Foo&gt; matches = delegate(Foo o){ return o.X &gt;= 20; });\nRemoveAll(matches);\n</code></pre>\n\n<p>3) <strong>Declaring a delegate explicitily and pointing to a function</strong></p>\n\n<pre><code>public delegate bool IsInValidEmployee (Employee emp);\n\nIsInValidEmployee invalidEmployeeDelegate = new IsInValidEmployee(IsEmployeeInValid);\nmyObjects.RemoveAll(myObject=&gt;invalidEmployeeDelegate(myObject);\n</code></pre>\n\n<p>// Actual function</p>\n\n<pre><code>public static bool IsEmployeeInValid(Employee emp)\n{\n    if (emp.Id &gt; 0 )\n        return true;\n    else\n        return false;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/59166', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/454247/']