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?

3 Answers 3

4

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

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

1 Comment

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.