3

I posted this question yesterday asking about a C# 4 client deploying an ASP.NET application to a .NET 3.5 target server and weather C# 3 on the server compiled something or C# 4 on the client.

That scenario was with optional parameters. I know optional parameters have been supported since like .NET 1.1. Why is it that C# 3 compiled IL code is not be able to call C#4 compiled IL code using optional parameters?

Does C# 3 just ignore the optional attribute?

Community
  • 1
  • 1
Joshua Enfield
  • 17,642
  • 10
  • 51
  • 98

3 Answers3

4

Optional parameters were introduced to C# only in version 4.0. (They were available in .NET earlier in VB).

Danny Varod
  • 17,324
  • 5
  • 69
  • 111
  • I am asking this question from the perspective of the IL Code. The way I understand it the optional parameters are applied using attributes that end up in the generated IL code. So my basically I am asking for clarification of what happens when C# 3 generated IL code tries to call code using Optional parameters in this case C# 4 generated IL, but I am guessing the same would hold for the earlier VB optionals i.e. C# 3 calling into VB generated IL code using optional or C# 3 calling into C# 4 generated IL code. – Joshua Enfield Jun 17 '11 at 13:52
2

You're right... The IL is just decorated w/ OptionalAttributes, but it's the calling compiler that decides what to do...

From Richter:

Now, when a compiler sees that you have code calling a method that is missing some arguments, the compiler can ensure that you've omitted optional argumements, grab their default values out of metadata, and embed them...

So it's the CALLING compiler deciding what to do w/ the optionals, not the called c# 4.0 IL...

More on edge cases and how it's the calling method that decides value can be found from Lippert, @ http://blogs.msdn.com/b/ericlippert/archive/2011/05/16/optional-argument-corner-cases-part-three.aspx

Rikon
  • 2,688
  • 3
  • 22
  • 32
1

My simple guess would be because the Base Class Libraries are different? This makes the difference between C# 3.* and 4.* much bigger than say 2.* vs 3.* as the latter used the same BCL.

Not sure though.

thekip
  • 3,660
  • 2
  • 21
  • 41