Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 258
Marek Kwiendacz Avatar asked Aug 09 '26 11:08

Marek Kwiendacz


2 Answers

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

This compiles to a runtime 2.0 compatible IL.

like image 180
Oded Avatar answered Aug 11 '26 02:08

Oded


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

like image 41
Tigran Avatar answered Aug 11 '26 01:08

Tigran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!