I am at chapter 9 of Jon Skeet's CSharp in Depth at a section which explains the improvements to type inference in 3.0
There is code snippet on Pg.247 that 'shouldn't compile with 2.0' - However I can't find a reason why it should not. Tried the problem with VS2008 C# Express Edition on a project with target framework as 2.0 - all three of the below calls compile and run too.
2.0 introduced the ability to infer the right type of delegate.
new List<ThreadStart>().Add( delegate { Console.WriteLine("New Thread!"); } ); // works
Of course Jon can't be wrong ( ;) 'Sql is broken' + there is no mention of this on the errata page for the book.) So my prime suspicion would be that it's still using 3.0 type inference - What am I missing ?
delegate int MyDelegate(string s);
delegate TOutput MyConverter<TInput, TOutput>(TInput input);
static void MyParse(MyDelegate del)
{
Console.WriteLine(del("100"));
}
static void MyConvert<TInput, TOutput>(MyConverter<TInput, TOutput> del, TInput input)
{
Console.WriteLine(del(input));
}
// Jon's code snippet begin
delegate T MyFunc<T>();
static void WriteResult<T>(MyFunc<T> function)
{
Console.WriteLine(function());
}
// end
static void Main(string[] args)
{
MyParse(delegate(string s)
{
return Int32.Parse(s);
}
);
MyConvert(delegate(string s)
{
return Int32.Parse(s);
},
"100");
WriteResult(delegate { return 5; }); // Jon: Shouldn't work.
}