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?

2

3 Answers 3

7

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

This compiles to a runtime 2.0 compatible IL.

Sign up to request clarification or add additional context in comments.

4 Comments

so it mean's that I can use for project targeted at 3.5 framework any new features from 4.0??
@MarekKwiendacz - Only if the compiler can produce correct 2.0 runtime IL.
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.
@MarekKwiendacz - The compiler will complain if you target the 2.0 runtime and it will not be able to compile compatible IL.
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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.