5

I am using Visual Studio 2010.

I've donwloaded an updated class (i.e. UpdatedClass.cs) which has a method with optional parameter, such as:

public void DoThis(bool aValue = false) {...}

Using Visual Studio 2010, I am able to compile it. But I cannot do it with MSBuild:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC>C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe /t:Rebuild /clp:ErrorsOnly D:\folder\mySolution.sln
ln
Microsoft (R) Build Engine Version 3.5.30729.5420
[Microsoft .NET Framework, Version 2.0.50727.5448]
Copyright (C) Microsoft Corporation 2007. All rights reserved.

UpdatedClass.cs(29,94): error CS0241: Default parameter specifiers are not permitted

Well, optional parameters are not permitted using this compiler. So can I add an extra argument in this MSBuild command, to ignore this kind of error?

Or do I have to compile the project using C:\WINDOWS\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe ? Is this safe regarding asp.net projects?

Junior Mayhé
  • 16,144
  • 26
  • 115
  • 161
  • Optional parameters are in the 4.0 framework, so you'll have to build it with the compatible compiler. (You'd see the same error in Visual Studio if you tried to build with a different target version.) – GalacticCowboy Mar 26 '12 at 16:20
  • 2
    @GalacticCowboy: Optional parameters have been in the *framework* since 1.0, but they weren't available in *C#* until v4. – Jon Skeet Mar 26 '12 at 16:21

1 Answers1

4

If you are using C# 4 syntax, you have to use a 4.0 compiler. And this means you need to run .Net 4.0 on the server where you intend to deploy this code. Alternatively - rewrite the code not to use default parameters, that's not hard - just add an overload like this:

public void DoThis() {DoThis(false);}
public void DoThis(bool aValue) {...}
skolima
  • 31,963
  • 27
  • 115
  • 151