3

In my project some developer added method that has optional parameter in parameters list:

public static string GeneratetPopupCall(string pageName,bool withEscapeChar = false)

I know that optional parameters are part of C# 4.0. But our project is targeted to .net 3.5. (C# 3.0)

My question is:
Why it compiles if 3.5 doesn't support optional parameters? Why that is no compilation or syntax error?

Marek Kwiendacz
  • 9,524
  • 14
  • 48
  • 72

3 Answers3

7

You are using the 4.0 compiler, targeted at the 3.5 framework.

This compiles to a runtime 2.0 compatible IL.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • so it mean's that I can use for project targeted at 3.5 framework any new features from 4.0?? – Marek Kwiendacz Feb 10 '12 at 14:40
  • @MarekKwiendacz - Only if the compiler can produce correct 2.0 runtime IL. – Oded Feb 10 '12 at 14:41
  • But native CLR for framework 4.0 is CLR 4. Is there any way to check if new features could be used in CLR 2. Other than try to compile & execute. – Marek Kwiendacz Feb 10 '12 at 14:45
  • @MarekKwiendacz - The compiler will complain if you target the 2.0 runtime and it will not be able to compile compatible IL. – Oded Feb 10 '12 at 14:47
2

As long as you use Visual Studio 2010, you can consume optional parameters even with older .NET Frameworks then 4.0.

The more information can find here

Tigran
  • 61,654
  • 8
  • 86
  • 123
1

Within Visual Studio you can specify the Language Version for a given project (Project Properties -> Build -> Advanced). Visual Studio uses the v4.0 compiler to target the v3.5 of the framework.

While this works, it can cause issues in other situations. For instance, an automated build environment which invokes a different version of the compiler will obviously fail. Just something to watch out for ...

Phil Klein
  • 7,344
  • 3
  • 30
  • 33