13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Changes to type <strong>in</strong>ference and overload resolution247If you remember, the type <strong>in</strong>ference rules of <strong>C#</strong> 2 were applied to each argument <strong>in</strong>dividually,with no way of us<strong>in</strong>g the types <strong>in</strong>ferred from one argument to another. In ourcase, these rules would have stopped us from f<strong>in</strong>d<strong>in</strong>g the types of TInput and TOutputfor the second argument, so the code <strong>in</strong> list<strong>in</strong>g 9.11 would have failed to compile.Our eventual goal is to understand what makes list<strong>in</strong>g 9.11 compile <strong>in</strong> <strong>C#</strong> 3 (and itdoes, I promise you), but we’ll start with someth<strong>in</strong>g a bit more modest.9.4.2 Inferred return types of anonymous functionsList<strong>in</strong>g 9.12 shows an example of some code that looks like it should compile butdoesn’t under the type <strong>in</strong>ference rules of <strong>C#</strong> 2.List<strong>in</strong>g 9.12Attempt<strong>in</strong>g to <strong>in</strong>fer the return type of an anonymous methoddelegate T MyFunc();static void WriteResult (MyFunc function){Console.WriteL<strong>in</strong>e(function()); Declares generic}method with...delegate parameterWriteResult(delegate { return 5; });Compil<strong>in</strong>g list<strong>in</strong>g 9.12 under <strong>C#</strong> 2 gives an errorDeclares Functhat isn’t <strong>in</strong> .NET 2.0Requires type<strong>in</strong>ference for Terror CS0411: The type arguments for method'Snippet.WriteResult(Snippet.MyFunc)' cannot be <strong>in</strong>ferred from theusage. Try specify<strong>in</strong>g the type arguments explicitly.We can fix the error <strong>in</strong> two ways—either specify the type argument explicitly (as suggestedby the compiler) or cast the anonymous method to a concrete delegate type:WriteResult(delegate { return 5; });WriteResult((MyFunc)delegate { return 5; });Both of these work, but they’re slightly ugly. We’d like the compiler to perform thesame k<strong>in</strong>d of type <strong>in</strong>ference as for nondelegate types, us<strong>in</strong>g the type of the returnedexpression to <strong>in</strong>fer the type of T. That’s exactly what <strong>C#</strong> 3 does for both anonymousmethods and lambda expressions—but there’s one catch. Although <strong>in</strong> many casesonly one return statement is <strong>in</strong>volved, there can sometimes be more. List<strong>in</strong>g 9.13 is aslightly modified version of list<strong>in</strong>g 9.12 where the anonymous method sometimesreturns an <strong>in</strong>teger and sometimes returns an object.List<strong>in</strong>g 9.13Code return<strong>in</strong>g an <strong>in</strong>teger or an object depend<strong>in</strong>g on the time of daydelegate T MyFunc();static void WriteResult (MyFunc function){Console.WriteL<strong>in</strong>e(function());}...Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!