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 7745433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:01:05+00:00 2026-06-01T10:01:05+00:00

I’m looking at how ParameterInfo.IsOptional is defined (I’m adding default parameter support to an

  • 0

I’m looking at how ParameterInfo.IsOptional is defined (I’m adding default parameter support to an internal IOC framework), and it seems to me that, when true, there is no guarantee that ParameterInfo.DefaultValue (or indeed ParameterInfo.RawDefaultValue) are actually the default values that are to be applied.

If you look at the MSDN example given for IsOptional, it seems possible in IL to define a parameter that is optional but for which no default is supplied (given that the ParameterAttributes.HasDefault must be explicitly supplied). I.e. potentially leading to a situation that a parameter type is, say, Int32, ParameterInfo.IsOptional is true, but ParameterInfo.DefaultValue is null.

My language is C#, therefore I can work on what that compiler will do. Based on that I can have a simple test as follows (parameter here is a ParameterInfo instance, and the method is meant to return an instance to be used as the runtime argument for the parameter):

if(no_config_value)
{
  if(!parameter.IsOptional) throw new InvalidOperationException();
  //it's optional, so read the Default
  return parameter.DefaultValue;
}
else
  return current_method_for_getting_value();

But I’m thinking that some languages (and I want to get this right at the IL-level, rather than just based on what one particular compiler does) can place the onus on the caller to determine the default value to be used, if so, a default(parameter.ParameterType) would need to be in order.

This is where it gets a little more interesting, because DefaultValue is, apparently DBNull.Value (according to the documentation for RawValue) if there is no default. Which is no good if the parameter is of type object and IsOptional==true!

Having done a bit more digging, I’m hopeful that the reliable way to solve this is to physically read the ParameterInfo.Attributes member, reading the bitflags individually first to check for ParameterAttributes.Optional and then check for ParameterAttributes.Default. Only if both are present, then reading ParameterInfo.DefaultValue will be correct.

I’m going to start coding and writing tests around this, but I’m asking in the hope that there’s someone with more IL knowledge that can confirm my suspicions and hopefully confirm that this’ll be correct for any IL-based language (thus avoiding the need to mock up loads of libraries in different languages!).

  • 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-06-01T10:01:06+00:00Added an answer on June 1, 2026 at 10:01 am

    The short answer to my question is no – just because IsOptional is true doesn’t mean that DefaultValue will actually contain the real default. My suppositions further down in the question text were correct (and the .Net documentation does kinda explain this, in a round-about way). In essence, if a default exists, then the caller should use it, otherwise the caller should provide it’s own default. The parameter’s Attributes are used to figure out if a default exists.

    This is what I’ve done.

    Assume the following method exists:

    /* wrapper around a generic FastDefault<T>() that returns default(T) */
    public object FastDefault(Type t) { /*elided*/ }
    

    And then given a particular parameter and Dictionary of supplied argument values (from configuration):

    public object GetParameterValue(ParameterInfo p, IDictionary<string, object> args)
    {
      /* null checks on p and args elided - args can be empty though */
      object argValue = null;
      if(args.TryGetValue(p.Name, out argValue))
        return argValue;
      else if(p.IsOptional)
      {
        //now check to see if a default is supplied in the IL with the method
        if((p.Attributes & ParameterAttributes.HasDefault) == 
            ParameterAttributes.HasDefault)
          return p.DefaultValue;  //use the supplied default
        else
          return FastDefault(p.ParameterType); //use the FastDefault method
      }
      else  //parameter requires an argument - throw an exception
        throw new InvalidOperationException("Parameter requires an argument");
    }
    

    I’ve then tested this logic on constructors and methods written like this:

    public class Test
    {
      public readonly string Message;
      public Test(string message = "hello") { Message = message; }
    }
    

    I.E, where a default is supplied in addition to the parameter being optional (the program correctly falls into the branch which reaches for ParameterInfo.DefaultValue).

    Then, in answer to another part of my question, I realised that in C# 4 we can use the OptionalAttribute to produce an optional parameter with no default:

    public class Test2
    {
      public readonly string Message;
      public Test2([OptionalAttribute]string message) { Message = message; }
    }
    

    Again, the program correctly falls into the branch which executes the FastDefault method.

    (In this case C# too will use the type’s default as the argument for this parameter)

    I think that covers it all – it’s working nicely on everything I’ve tried (I have had fun trying to get overload resolution feeling correct as my IOC system always uses the equivalent of named arguments – but the C# 4 spec helped there).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I need a function that will clean a strings' special characters. I do NOT
I am writing an app with both english and french support. The app requests

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.