<p>What is the best way to find something in a list?  I know LINQ has some nice tricks, but let's also get suggestions for C# 2.0.  Lets get the best refactorings for this common code pattern.</p>

<p>Currently I use code like this:</p>

<pre><code>// mObjList is a List&lt;MyObject&gt;
MyObject match = null;
foreach (MyObject mo in mObjList)
{
    if (Criteria(mo))
    {
        match = mo;
        break;
    }
}
</code></pre>

<p>or</p>

<pre><code>// mObjList is a List&lt;MyObject&gt;
bool foundIt = false;
foreach (MyObject mo in mObjList)
{
    if (Criteria(mo))
    {
        foundIt = true;
        break;
    }
}
</code></pre>

[{'answer_id': 23794, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 1, 'selected': False, 'text': '<p>Put the code in a method and you save a temporary and a <code>break</code> (and you recycle code, as a bonus):</p>\n\n<pre><code>T Find&lt;T&gt;(IEnumerable&lt;T&gt; items, Predicate&lt;T&gt; p) {\n    foreach (T item in items)\n        if (p(item))\n            return item;\n\n    return null;\n}\n</code></pre>\n\n<p>… but of course this method already exists anyway for Lists, even in .NET 2.0.</p>\n'}, {'answer_id': 23799, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 5, 'selected': True, 'text': "<blockquote>\n  <p>@ Konrad: So how do you use it? Let's say I want to match mo.ID to magicNumber.</p>\n</blockquote>\n\n<p>In C# 2.0 you'd write:</p>\n\n<pre><code>result = mObjList.Find(delegate(int x) { return x.ID == magicNumber; });\n</code></pre>\n\n<p>3.0 knows lambdas:</p>\n\n<pre><code>result = mObjList.Find(x =&gt; x.ID == magicNumber);\n</code></pre>\n"}, {'answer_id': 23800, 'author': 'Todd', 'author_id': 2572, 'author_profile': 'https://Stackoverflow.com/users/2572', 'pm_score': 2, 'selected': False, 'text': '<p>Using a Lambda expression:</p>\n\n<pre><code>List&lt;MyObject&gt; list = new List&lt;MyObject&gt;();\n\n// populate the list with objects..\n\nreturn list.Find(o =&gt; o.Id == myCriteria);\n</code></pre>\n'}, {'answer_id': 23830, 'author': 'Nick', 'author_id': 1490, 'author_profile': 'https://Stackoverflow.com/users/1490', 'pm_score': 1, 'selected': False, 'text': '<p>Evidently the performance hit of anonymous delegates is pretty significant.</p>\n\n<p>Test code:</p>\n\n<pre><code>    static void Main(string[] args)\n    {\n        for (int kk = 0; kk &lt; 10; kk++)\n        {\n            List&lt;int&gt; tmp = new List&lt;int&gt;();\n            for (int i = 0; i &lt; 100; i++)\n                tmp.Add(i);\n            int sum = 0;\n            long start = DateTime.Now.Ticks;\n            for (int i = 0; i &lt; 1000000; i++)\n                sum += tmp.Find(delegate(int x) { return x == 3; });\n            Console.WriteLine("Anonymous delegates: " + (DateTime.Now.Ticks - start));\n\n\n            start = DateTime.Now.Ticks;\n            sum = 0;\n            for (int i = 0; i &lt; 1000000; i++)\n            {\n                int match = 0;\n                for (int j = 0; j &lt; tmp.Count; j++)\n                {\n                    if (tmp[j] == 3)\n                    {\n                        match = tmp[j];\n                        break;\n                    }\n                }\n                sum += match;\n            }\n            Console.WriteLine("Classic C++ Style: " + (DateTime.Now.Ticks - start));\n            Console.WriteLine();\n        }\n    }\n</code></pre>\n\n<p>Results:</p>\n\n<pre><code>Anonymous delegates: 710000\nClassic C++ Style: 340000\n\nAnonymous delegates: 630000\nClassic C++ Style: 320000\n\nAnonymous delegates: 630000\nClassic C++ Style: 330000\n\nAnonymous delegates: 630000\nClassic C++ Style: 320000\n\nAnonymous delegates: 610000\nClassic C++ Style: 340000\n\nAnonymous delegates: 630000\nClassic C++ Style: 330000\n\nAnonymous delegates: 650000\nClassic C++ Style: 330000\n\nAnonymous delegates: 620000\nClassic C++ Style: 330000\n\nAnonymous delegates: 620000\nClassic C++ Style: 340000\n\nAnonymous delegates: 620000\nClassic C++ Style: 400000\n</code></pre>\n\n<p>In every case, using anonymous delegates is about 100% slower than the other way.</p>\n'}]
['https://Stackoverflow.com/questions/23787', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1490/']