Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is no way to do this, because Method Resolution always works on combination of return type and parameters but not only on return type. The reason I just posted in other post, here is the problem. The reason why C# does not offer method resolution by only different return type is following...</p> <pre><code>public class Test{ public int DoMethod(){ return 2; } public string DoMethod() { return "Name"; } } Test t; int n = t.DoMethod(); // 1st method string txt = t.DoMethod(); // 2nd method object x = t.DoMethod(); // DOOMED ... which one?? </code></pre> <p>If you notice, in last statement, the programming language has no correct solution of which method to choose.</p> <p>This is the exact same reason the following code does not work...</p> <pre><code>public class Test{ public T ReturnEmpty&lt;T&gt;() { return default(T); } public T ReturnEmpty&lt;T&gt;(T x) { return x; } } Test t = new Test(); int n = t.ReturnEmpty(); &lt;--- does not work.. the reson check next statement... object k = t.ReturnEmpty(); &lt;--- which one... int l = t.ReturnEmpty&lt;int&gt;(); &lt;--- this is correct.... string m = t.ReturnEmpty("Do Test"); &lt;-- this is correct... </code></pre> <p>So once again, same solution, resolving method based only on return type is an abigious solution for any compiler. Programming languages are different from spoken languages because they are designed to always give "One Execution Way" only, no ambiguity and no context dependency. Every statement works exactly independent of the context.</p> <p>However you can argue that you have correct return type, but that may not always be the case, compilers need strong rules to compile and generate code.</p>
 

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