Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy isn't inlining a method equivalent to declaring it explicitly?
    primarykey
    data
    text
    <h2>Do you have a blind spot in programming?</h2> <p>I mean is there a common technique or language feature that you can't really get used to. Well, I have one (or probably more than one) and mine is usage of <code>delegate</code>. Hands up! Who else doesn't feel comfortable with delegates? Be honest!</p> <h2>So what's a delegate?</h2> <p>Since my courses at university introduced me to C, I know about function pointers. Function pointers are handy if you want to pass methods as arguments. So in my mind a delegate is something like a function pointer. Eureka! I got it. I have not!</p> <h2>A concrete scenario?</h2> <p>I would like to remove any line from a text file that matches a <a href="http://en.wikipedia.org/wiki/Regular_expression" rel="nofollow noreferrer">regular expression</a>. Assuming I have a collection of lines, <code>List&lt;T&gt;</code> has method <code>RemoveAll</code> which seems to be perfectly suitable for that purpose. <code>RemoveAll</code> expects an evaluation method as argument for deciding on whether to remove or leave a list element. And there it is: The function pointer!</p> <h2>Any code here?</h2> <pre><code>public static int RemoveLinesFromFile(string path, string pattern) { List&lt;string&gt; lines = new List&lt;string&gt;(File.ReadAllLines(path)); int result = lines.RemoveAll(DoesLineMatch); File.WriteAllLines(path, lines.ToArray()); return result; } </code></pre> <p>So I'm looking for a function <code>DoesLineMatch</code> which evaluates if a line matches a pattern.</p> <h2>Do you see the problem?</h2> <p><code>RemoveAll</code> expects a delegate <code>Predicate&lt;string&gt; match</code> as argument. I would have coded it like this:</p> <pre><code>private static bool DoesLineMatch(string line, string pattern) { return Regex.IsMatch(line, pattern); } </code></pre> <p>But then I'm getting an error "Expected a method with 'bool DoesLineMatch(string)' signature". What am I missing here?</p> <h2>Does it work at all?</h2> <p>This is how I finally got it working:</p> <pre><code>public static int RemoveLinesFromFile(string path, string pattern) { List&lt;string&gt; lines = new List&lt;string&gt;(File.ReadAllLines(path)); int result = lines.RemoveAll(delegate(string line) { return Regex.IsMatch(line, pattern); }); File.WriteAllLines(path, lines.ToArray()); return result; } </code></pre> <p>I'm happy that it works but I don't understand it.</p> <h2>And what is the question?</h2> <p>What I did to get it working is simply inlining the method. As far as I understand inlining, it is just some kind of use-once-and-destroy-code. If you use a variable or method only once you may inline it, but inlining is always equivalent to declaring it explicitly.</p> <p><strong>Is there a way to declare the method explicitly? How would I do it?</strong></p> <p>PS.: Pardon me that my question is somewhat lengthy.</p> <p>PPS.: As soon as I get this delegate thing I will make the leap from 2.0 to 3.0 and learn lambdas.</p> <p>PPPS.: Following <a href="https://stackoverflow.com/questions/1508774/why-isnt-inlining-a-method-equivalent-to-declaring-it-explicitly/1508802#1508802">Jon's hint</a> on efficiency of <code>Regex.IsMatch(string, string)</code> I modified my code:</p> <pre><code> int result = lines.RemoveAll(delegate(string line) { Regex regex = new Regex(pattern); return regex.IsMatch(line); }); </code></pre> <p>That isn't of much help regarding efficiency matters. So I followed <a href="http://en.wikipedia.org/wiki/ReSharper" rel="nofollow noreferrer">ReSharper</a>'s proposal and moved the Regex instantiation to the outer scope:</p> <pre><code> Regex regex = new Regex(pattern); int result = lines.RemoveAll(delegate(string line) { return regex.IsMatch(line); }); </code></pre> <p>Now ReSharper urged me to replace this with a method group:</p> <pre><code> Regex regex = new Regex(pattern); int result = lines.RemoveAll(regex.IsMatch); </code></pre> <p>And that is quite similar to the answers proposed here. Not what I asked for, but again I'm amazed how ReSharper (and Stack Overflow of course) helps learning.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload