How does the method overload processing system decide which method to call when passing an empty value?

So, for example, you have a type type:

public class EffectOptions { public EffectOptions ( params object [ ] options ) {} public EffectOptions ( IEnumerable<object> options ) {} public EffectOptions ( string name ) {} public EffectOptions ( object owner ) {} public EffectOptions ( int count ) {} public EffectOptions ( Point point ) {} } 

Here I just give an example using constructors, but the result would be the same if they were non-design methods for the type itself, right?

So when you do this:

 EffectOptions options = new EffectOptions (null); 

which constructor will be called, and why?

I could check it out myself, but I want to understand how the overload resolution system works (not sure what it called).

+30
c # overload-resolution
Mar 02 2018-11-11T00:
source share
3 answers

For exact rules, see the overload resolution specification . But in short, this happens as follows.

First create a list of all available constructors.

 public EffectOptions ( params object [ ] options ) public EffectOptions ( IEnumerable<object> options ) public EffectOptions ( string name ) public EffectOptions ( object owner ) public EffectOptions ( int count ) public EffectOptions ( Point point ) 

Then eliminate any inappropriate constructors. An applicable constructor is one where each formal parameter has a corresponding argument, and the argument is implicitly converted to the formal parameter type. Assuming Point is a value type, we exclude versions of "int" and "Point". It leaves

 public EffectOptions ( params object[] options ) public EffectOptions ( IEnumerable<object> options ) public EffectOptions ( string name ) public EffectOptions ( object owner ) 

Now we have to consider whether one who has "params" in its expanded or unexpanded form is applicable. In this case, this applies in both forms. When this happens, we discard the expanded form. So the leaves

 public EffectOptions ( object[] options ) public EffectOptions ( IEnumerable<object> options ) public EffectOptions ( string name ) public EffectOptions ( object owner ) 

Now we must determine the best candidates. Bestselling rules are complex, but the short version is more specific than less specific . A giraffe is more specific than a mammal, a mammal is more specific than an animal, an animal is more specific than an object.

The "object" version is less specific than all of them, so it can be eliminated. The IEnumerable<object> version is less specific than the object[] version (you see why?), So it can also be fixed. It leaves

 public EffectOptions ( object[] options ) public EffectOptions ( string name ) 

And now we are stuck. object [] is no more or less specific than a string. Therefore, this gives an ambiguity error.

This is just a brief sketch; the real tiebreaking algorithm is much more complicated. But these are the basics.

+68
Mar 02 '11 at 23:20
source share

In this case, the C # compiler will not select any constructor and will instead be an error. The null value is legal for several available constructors, and the fault logic is not enough to select it, so it causes an error.

The C # compiler overload resolution logic is a complex process, but a short (and inherently incomplete) overview of how it works is as follows

  • Collect all members with the specified name
  • Filter items with parameter lists that are compatible with the provided arguments and with the appropriate availability
  • If the other members have more than one element, use the binding logic to select the best ones.

See Section 7.4 of the C # Language Specification for complete details. And I'm sure Eric will be off soon to give a much more accurate description :)

+7
Mar 02 2018-11-11T00:
source share

While this article is marked with VB, I find the C # functions are similar. As Jared said in his answer, if he cannot choose the overload, he will throw an error.

http://msdn.microsoft.com/en-us/library/tb18a48w(v=VS.100).aspx

+1
Mar 02 2018-11-21T00:
source share



All Articles