Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4257876
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:34:22+00:00 2026-05-21T05:34:22+00:00

I found the optional parameters feature in C# 4.0 very interesting, so I tried

  • 0

I found the “optional parameters” feature in C# 4.0 very interesting, so I tried to figure out how they made it happen.
so I wrote a method like this:

private static void A(int a = 5) { }

Compiled it, then decompiled it in IL DASM, this is the IL code:

.method private hidebysig static void  A([opt] int32 a) cil managed
{
  .param [1] = int32(0x00000005)
  // Code size       2 (0x2)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  ret
} // end of method Program::A

And it has got this in its metadata:

(1) ParamToken : (08000002) Name : a flags: [Optional] [HasDefault] (00001010) Default: (I4) 5

So I followed the clue and wrote a method like this:

private static void B([Optional, DefaultParameterValue(78)]int b) { }

Compiled it and decompiled it, and I found that the C# compiler generated almost the identical MSIL code for method A and B(except for the name).

As we can see there is no sign of attributes in the IL code and it felt wrong, so I wrote a custom attribute like this:

[AttributeUsage(AttributeTargets.Parameter)]
public class MyTestAttribute : Attribute
{
}

Then used it in method C like this:

private static void C([MyTest]int c) { }

Compiled it and then decompiled it, and hah, I found this:

.method private hidebysig static void  C(int32 c) cil managed
{
  .param [1]
  .custom instance void ConsoleApplication1.MyTestAttribute::.ctor() = ( 01 00 00 00 ) 
  // Code size       2 (0x2)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  ret
} // end of method Program::C

The second line of the method body calls to the ctor of my custom attribute.

So this leads to my doubts:

  1. What does [opt] mean? I mean the one that appears in front of method A and B’s parameter.
  2. How come method C calls the constructor of the Attribute that is applied to its parameter and method A and B do not?
  3. I can not seem to find any sign of DefaultParameterValueAttribute in the metadata, but I can find OptionalAttribute and MyTestAttribute. Why is that? Is there something that I am missing?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:34:23+00:00Added an answer on May 21, 2026 at 5:34 am

    The C# compiler doesn’t need to emit the attributes since the Param metadata table can already describe optional and default values via the Flags column.

    From 23.1.13 in ECMA 335:

    Flag            Value   Description
    -----------------------------------------------------
    In              0x0001  Parameter is [In]  
    Out             0x0002  Parameter is [Out]  
    Optional        0x0010  Parameter is optional  
    HasDefault      0x1000  Parameter has a default value  
    HasFieldMarshal 0x2000  Parameter has FieldMarshal  
    

    A parameter can have a flag value that specifies it is optional and has a default value (0x0010 | 0x1000). Parameters that have a default value will have an associated token in the Constant metadata table.

    The Constant metadata table has a Parent column that would be the Param token in question and a Value column that would be an index into the blob heap where the default value is stored.

    So to answer your questions:

    1. [opt] means the Flags column for the Param token has the Optional flag set.
    2. As I stated above, my guess here is that the C# compiler is recognizing the Optional/DefaultParameterValue attributes and simply converting them to parameter flags.
    3. Edit: It appears that the C# compiler is emitting an unused TypeRef for OptionalAttribute, despite the Optional flag being used for the parameter. It doesn’t emit a TypeRef for DefaultParameterValueAttribute, though. It could be a small compiler bug for emitting unused TypeRefs/MemberRefs.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this interesting thing when I was trying out the new feature optional
I added 3 optional boolean parameters to a method found within a VB6 DLL.
I have URLs like http://example.com/depict?smiles=CO&width=200&height=200 (and with several other optional arguments) My urls.py contains:
I know the XMLHttpRequest object supports a method open which has an optional parameter
I am working on a stored procedure with several optional parameters. Some of these
Is there way to call an XSL template with optional parameters? For example: <xsl:call-template
I cannot rewrite to include two parameters but the last parameter is optional, so
I've searched the net but didn't found anything interesting. Maybe I'm doing something wrong.
I'm trying to allow flexible method parameters inside of my WCF service, but I've
This is, probably, a very simple answer for someone. I have a method with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.