Now, look at
the types of the arguments we??™re calling it with in the bottom line of the listing. The
first argument is clearly a string, but what about the second? It??™s a lambda expression,
so we need to convert it into a Converter
??”and that means we
need to know the types of TInput and TOutput.
Listing 9.11 Example of code requiring the new type inference rules
247 Changes to type inference and overload resolution
If you remember, the type inference rules of C# 2 were applied to each argument individually,
with no way of using the types inferred from one argument to another. In our
case, these rules would have stopped us from finding the types of TInput and TOutput
for the second argument, so the code in listing 9.11 would have failed to compile.
Our eventual goal is to understand what makes listing 9.11 compile in C# 3 (and it
does, I promise you), but we??™ll start with something a bit more modest.
9.4.2 Inferred return types of anonymous functions
Listing 9.12 shows an example of some code that looks like it should compile but
doesn??™t under the type inference rules of C# 2.
delegate T MyFunc();
static void WriteResult (MyFunc function)
{
Console.WriteLine(function());
}
...
WriteResult(delegate { return 5; });
Compiling listing 9.12 under C# 2 gives an error
error CS0411: The type arguments for method
'Snippet.
Pages:
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473