13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Changes to type <strong>in</strong>ference and overload resolution251phase 2, TMiddle is fixed to <strong>in</strong>t and we <strong>in</strong>fer that there must be a conversion fromdouble to TOutput. The third time through phase 2, TOutput is fixed to double andtype <strong>in</strong>ference succeeds. When type <strong>in</strong>ference has f<strong>in</strong>ished, the compiler can look atthe code with<strong>in</strong> the lambda expression properly.NOTECheck<strong>in</strong>g the body of a lambda expression—The body of a lambda expressioncannot be checked until the <strong>in</strong>put parameter types are known. The lambdaexpression x => x.Length is valid if x is an array or a str<strong>in</strong>g, but <strong>in</strong>valid <strong>in</strong>many other cases. This isn’t a problem when the parameter types areexplicitly declared, but with an implicit parameter list the compiler needsto wait until it’s performed the relevant type <strong>in</strong>ference before it can try towork out what the lambda expression means.These examples have shown only one change work<strong>in</strong>g at a time—<strong>in</strong> practice there canbe several pieces of <strong>in</strong>formation about different type variables, potentially discovered<strong>in</strong> different iterations of the process. In an effort to save your sanity (and m<strong>in</strong>e), I’mnot go<strong>in</strong>g to present any more complicated examples—hopefully you understand thegeneral mechanism, even if the exact details are hazy.Although it may seem as if this k<strong>in</strong>d of situation will occur so rarely that it’s notworth hav<strong>in</strong>g such complex rules to cover it, <strong>in</strong> fact it’s quite common <strong>in</strong> <strong>C#</strong> 3, particularlywith LINQ. Indeed, you could easily use type <strong>in</strong>ference extensively without eventh<strong>in</strong>k<strong>in</strong>g about it—it’s likely to become second nature to you. If it fails and you wonderwhy, however, you can always revisit this section and the language specification.There’s one more change we need to cover, but you’ll be glad to hear it’s easierthan type <strong>in</strong>ference: method overload<strong>in</strong>g.9.4.4 Pick<strong>in</strong>g the right overloaded methodOverload<strong>in</strong>g occurs when there are multiple methods available with the same namebut different signatures. Sometimes it’s obvious which method is appropriate, becauseit’s the only one with the right number of parameters, or it’s the only one where allthe arguments can be converted <strong>in</strong>to the correspond<strong>in</strong>g parameter types.The tricky bit comes when there are multiple methods that could be the right one.The rules are quite complicated (yes, aga<strong>in</strong>)—but the key part is the way that eachargument type is converted <strong>in</strong>to the parameter type. For <strong>in</strong>stance, consider thesemethod signatures, as if they were both declared <strong>in</strong> the same type:void Write(<strong>in</strong>t x)void Write(double y)The mean<strong>in</strong>g of a call to Write(1.5) is obvious, because there’s no implicit conversionfrom double to <strong>in</strong>t, but a call to Write(1) is trickier. There is an implicitconversion from <strong>in</strong>t to double, so both methods are possible. At that po<strong>in</strong>t, thecompiler considers the conversion from <strong>in</strong>t to <strong>in</strong>t, and from <strong>in</strong>t to double. A conversionfrom any type to itself is def<strong>in</strong>ed to be better than any conversion to a differenttype, so the Write(<strong>in</strong>t x) method is better than Write(double y) for thisparticular call.Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!