4

I found this interesting thing when I was trying out the new feature “optional parameters” in C# 4.0.

I know that there are two ways to use “optional parameters” in C# 4.0:

static void TestMethod(int parameter = 5) { }
static void TestMethod2([Optional, DefaultParameterValue(5)]int parameter) { }

Now if I compile this code and then view the assembly using IL Dasm, then I’ll see that in the “MetaInfo” window of IL Dasm, there is a type reference to OptionalAttribute like this:

Token:             0x01000002
ResolutionScope:   0x23000001
TypeRefName:       System.Runtime.InteropServices.OptionalAttribute
MemberRef #1 (0a000001)
-------------------------------------------------------
    Member: (0a000001) .ctor: 
    CallCnvntn: [DEFAULT]
    hasThis 
    ReturnType: Void
    No arguments.

But there is no sign of DefaultParameterValueAttribute. Why is that?

Actually I think the two attributes both should not be here, because they are treated differently by the compiler, they have their own flag values. To explain what I mean, please take a look at this:

    Method #2 (06000002) 
-------------------------------------------------------
    MethodName: TestMethod (06000002)
    Flags     : [Private] [Static] [HideBySig] [ReuseSlot]  (00000091)
    RVA       : 0x00002053
    ImplFlags : [IL] [Managed]  (00000000)
    CallCnvntn: [DEFAULT]
    ReturnType: Void
    1 Arguments
        Argument #1:  I4
    1 Parameters
        (1) ParamToken : (08000002) Name : parameter flags: [Optional] [HasDefault]  (00001010) Default: (I4) 5

Method #3 (06000003) 
-------------------------------------------------------
    MethodName: TestMethod2 (06000003)
    Flags     : [Private] [Static] [HideBySig] [ReuseSlot]  (00000091)
    RVA       : 0x00002056
    ImplFlags : [IL] [Managed]  (00000000)
    CallCnvntn: [DEFAULT]
    ReturnType: Void
    1 Arguments
        Argument #1:  I4
    1 Parameters
        (1) ParamToken : (08000003) Name : parameter flags: [Optional] [HasDefault]  (00001010) Default: (I4) 5

That is the metadata of the two methods. We can see that the last line of each code section already has the default parameter value stored, so why is OptionalAttribute still referenced?

1 Answer 1

3

This can be considered as a bug in the compiler. If you examine the assembly closely, there's nothing that references the OptionalAttribute TypeRef.

The TypeRef won't be present in the resulting assembly if you don't explicitely add in it TestMethod2. This simply shows that the attribute is added in a first pass, and later on removed. And it also shows that DefaultParameterValueAttribute is handled differently by the compiler.

Ultimately, this really is a small implementation detail, for instance, Mono's C# compiler won't produce a TypeRef for either attribute in the resulting assembly.

1
  • Thanks. That's what I was guessing. And salute to the mono team :) Apr 13, 2011 at 6:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.