Question

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.
    }
Was it helpful?

Solution

What the 'target framework' setting does, is disable libraries and methods that have been added in newer framework versions. Most of the new language features don't require special runtime support or new libraries. So if you use them, you need the newer compiler, but your app will still work on 2.0.

OTHER TIPS

I've got Visual Studio 2005 installed on my computer and I just tried out the code that you've posted.

Both MyConvert and WriteResult don't work. I get the following errors:

The type arguments for method 'ConsoleApplication1.Program.MyConvert<TInput,TOutput>(ConsoleApplication1.Program.MyConverter<TInput,TOutput>, TInput)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

and

The type arguments for method 'ConsoleApplication1.Program.WriteResult<T>(ConsoleApplication1.Program.MyFunc<T>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Don't have my copy of Jon's book, but he'll probably be along soon!

What section is this in? Try looking it up here: http://csharpindepth.com/Notes.aspx

EDIT: Actually, targeting the framework version has nothing to do with the compiler version. So you are still using C# 3.0. The compiler just has to figure out what IL to generate such that it will run on CLR 2.0, and so it can perform as much inference as it likes to do that.

There are several different versions involved here:

  • The IDE: Visual Studio 2008
  • The .NET framework: you're targeting .NET 2.0
  • The CLR: you're targeting 2.0 (ignoring service packs :)
  • The C# compiler you're using: Visual Studio 2008 always uses the C# 3.0 compiler
  • The language version the compiler is targeting: 3.0 by default

To see type inference fail without going back to the version 2.0 compiler, you need to make the C# compiler use version 2 of the language. I don't know if the express edition of Visual Studio exposes this, but you can use the command line to see it.

Unfortunately - and this is really weird - I now can't reproduce the difficulty that way either.

Here's what I just tried:

using System;

class Test
{
    public delegate T Function<T>();
    public static T Execute<T>(Function<T> function)
    {
        return function();
    }

    static void Main()
    {
        Execute(delegate { return 5; });
    }
}

I'd expected that compiling with

csc /langversion:ISO-2 Test.cs

would fail... but it doesn't. This does fail with the real C# 2 compiler:

>c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc Test.cs
Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.3053
for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.

Test.cs(14,9): error CS0411: The type arguments for method
        'Test.Execute<T>(Test.Function<T>)' cannot be
        inferred from the usage. Try specifying the type 
        arguments explicitly.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top