1

I am working with an ASP.NET Web Site project targeting .NET framework version 3.5 and I am wondering if it is possible to use the C# compiler for C#4. At the moment I don't have the time to upgrade the whole site to .NET framework 4 but I want to be able to use for instance optional parameters. As far as I know, optional parameters is not a framework 4 feature.

It this is possible, what changes to web.config must be done? (I guess it is a configuration issue.)

  • You cannot use C# 4 with while running and targeting .NET 3.5 – Security Hound Apr 15 '11 at 14:56
  • possible duplicate of [Can you use Optional Parameters in code targeting .Net 3.5?](http://stackoverflow.com/questions/1210679/can-you-use-optional-parameters-in-code-targeting-net-3-5) – ChrisF Apr 15 '11 at 14:56
  • That is not a duplicate. He's asking about ASP.Net compilation. – SLaks Apr 15 '11 at 14:56

1 Answers1

3

Try adding the following to Web.config

<configuration>
    <system.codedom>
        <compilers>
                <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4"
                                  type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
                        <providerOption name="CompilerVersion" value="v4.0"/>
                </compiler>
        </compilers>
    </system.codedom>
</configuration>
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Thanks for your reply (and for your comments to the other comments about this being a duplicate). I have tried that and I am getting the error "Default parameter specifiers are not permitted". But it feels like this is the right place, if it is at all possible. Is there a new version of the CSharpCodeProvider that should be used? – Mikael Jirhage Apr 15 '11 at 16:00
  • I don't know. The CSharpCodeProvider in System.dll v4 might help, but that would require that you be running .Net 4.0 (at runtime), which you don't want). – SLaks Apr 15 '11 at 16:04
  • What exactly does the error look like? Did you get a different error without this markup? – SLaks Apr 15 '11 at 16:05
  • If I build the site the error message (in the Error list window) is "Default parameter specifiers are not permitted" and if I hover over the code the tooltip says "Feature 'optional parameter' cannot be used because it is not part of the 3.0 C# language specification." If I ran the site I get a yellow screen of death with the error "'v4.0' is not a valid value for attribute 'system.codedom/compilers/compiler/ProviderOption/CompilerVersion'. – Mikael Jirhage Apr 18 '11 at 11:10
  • If I change the CompilerVersion back to 3.5 I get a yellow screen of death with the error "Default parameter specifiers are not permitted". – Mikael Jirhage Apr 18 '11 at 11:17