Marek Kwiendacz
Marek Kwiendacz

Reputation: 9834

Strange compiler behaviour - optional parameter

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?

Upvotes: 3

Views: 247

Answers (3)

Phil Klein
Phil Klein

Reputation: 7514

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 ...

Upvotes: 1

Tigran
Tigran

Reputation: 62246

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

Upvotes: 2

Oded
Oded

Reputation: 499002

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

This compiles to a runtime 2.0 compatible IL.

Upvotes: 7

Related Questions