Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>EDIT: I think I've got it.</p> <p>As zinglon says, it's because there's an implicit conversion from <code>GetString</code> to <code>Action</code> even though the compile-time application would fail. Here's the introduction to section 6.6, with some emphasis (mine):</p> <blockquote> <p>An implicit conversion (§6.1) exists from a method group (§7.1) to a compatible delegate type. Given a delegate type D and an expression E that is classified as a method group, an implicit conversion exists from E to D if E contains at least one method that is <strong>applicable in its normal form (§7.4.3.1) to an argument list constructed by use of the parameter types and modifiers of D</strong>, as described in the following.</p> </blockquote> <p>Now, I was getting confused by the first sentence - which talks about a conversion to a compatible delegate type. <code>Action</code> is not a compatible delegate for any method in the <code>GetString</code> method group, but the <code>GetString()</code> method <em>is</em> applicable in its normal form to an argument list constructed by use of the parameter types and modifiers of D. Note that this <em>doesn't</em> talk about the return type of D. That's why it's getting confused... because it would only check for the delegate compatibility of <code>GetString()</code> when <em>applying</em> the conversion, not checking for its existence.</p> <p>I think it's instructive to leave overloading out of the equation briefly, and see how this difference between a conversion's <em>existence</em> and its <em>applicability</em> can manifest. Here's a short but complete example:</p> <pre><code>using System; class Program { static void ActionMethod(Action action) {} static void IntMethod(int x) {} static string GetString() { return ""; } static void Main(string[] args) { IntMethod(GetString); ActionMethod(GetString); } } </code></pre> <p>Neither of the method invocation expressions in <code>Main</code> compiles, but the error messages are different. Here's the one for <code>IntMethod(GetString)</code>:</p> <blockquote> <p>Test.cs(12,9): error CS1502: The best overloaded method match for 'Program.IntMethod(int)' has some invalid arguments</p> </blockquote> <p>In other words, section 7.4.3.1 of the spec can't find any applicable function members.</p> <p>Now here's the error for <code>ActionMethod(GetString)</code>:</p> <blockquote> <p>Test.cs(13,22): error CS0407: 'string Program.GetString()' has the wrong return type</p> </blockquote> <p>This time it's worked out the method it wants to call - but it's failed to then perform the required conversion. Unfortunately I can't find out the bit of the spec where that final check is performed - it looks like it <em>might</em> be in 7.5.5.1, but I can't see exactly where.</p> <hr> <p>Old answer removed, except for this bit - because I expect Eric could shed light onto the "why" of this question...</p> <p>Still looking... in the mean time, if we say "Eric Lippert" three times, do you think we'll get a visit (and thus an answer)?</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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