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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:20:46+00:00 2026-06-16T09:20:46+00:00

Given a class like this: public abstract class SomeClass { private string _name; private

  • 0

Given a class like this:

public abstract class SomeClass
  {
      private string _name;
      private int _someInt;
      private int _anotherInt;

      public SomeClass(string name, int someInt = 10, int anotherInt = 20)
      {
          _name = name;
          _someInt = someInt;
          _anotherInt = anotherInt;
      }
  }

Is it possible using reflection to get the optional parameter’s default values?

  • 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-16T09:20:48+00:00Added an answer on June 16, 2026 at 9:20 am

    Lets take a basic program:

    class Program
    {
        static void Main(string[] args)
        {
            Foo();
        }
        public static void Foo(int i = 5)
        {
            Console.WriteLine("hi"   +i);
        }
    }
    

    And look at some IL Code.

    For Foo:

    .method public hidebysig static void  Foo([opt] int32 i) cil managed
    {
      .param [1] = int32(0x00000005)
      // Code size       24 (0x18)
      .maxstack  8
      IL_0000:  nop
      IL_0001:  ldstr      "hi"
      IL_0006:  ldarg.0
      IL_0007:  box        [mscorlib]System.Int32
      IL_000c:  call       string [mscorlib]System.String::Concat(object,
                                                                  object)
      IL_0011:  call       void [mscorlib]System.Console::WriteLine(string)
      IL_0016:  nop
      IL_0017:  ret
    } // end of method Program::Foo
    

    For Main:

    .method private hidebysig static void  Main(string[] args) cil managed
    {
      .entrypoint
      // Code size       9 (0x9)
      .maxstack  8
      IL_0000:  nop
      IL_0001:  ldc.i4.5
      IL_0002:  call       void ConsoleApplication3.Program::Foo(int32)
      IL_0007:  nop
      IL_0008:  ret
    } // end of method Program::Main
    

    Notice that main has 5 hardcoded as part of the call, and in Foo. The calling method is actually hardcoding the value that is optional! The value is at both the call-site and callee site.

    You will be able to get at the optional value by using the form:

    typeof(SomeClass).GetConstructor(new []{typeof(string),typeof(int),typeof(int)})
    .GetParameters()[1].RawDefaultValue

    On MSDN for DefaultValue (mentioned in the other answer):

    This property is used only in the execution context. In the reflection-only context, use the RawDefaultValue property instead. MSDN

    And finally a POC:

    static void Main(string[] args)
    {
       var optionalParameterInformation = typeof(SomeClass).GetConstructor(new[] { typeof(string), typeof(int), typeof(int) })
       .GetParameters().Select(p => new {p.Name,  OptionalValue = p.RawDefaultValue});
    
       foreach (var p in optionalParameterInformation)
          Console.WriteLine(p.Name+":"+p.OptionalValue);
    
       Console.ReadKey();
    }
    

    http://bartdesmet.net/blogs/bart/archive/2008/10/31/c-4-0-feature-focus-part-1-optional-parameters.aspx

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

Sidebar

Related Questions

I have an abstract class like this public abstract class Abstractclass { public Abstractclass(int
Given a simple C# class definition like: [System.Windows.Markup.ContentProperty(PropertyOne)] public class SimpleBase { public string
Given a class like this: class B class << self attr_accessor :var end end
The essence of the problem is, given a class hierarchy like this: class A
Given a class with a member template function like this one: template <typename t>
Given: class example { public: std::vector<std::vector<int>> a; int b; } func() { example e;
Given the following class structure: @MappedSuperclass @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) public abstract class Animal {} @Entity public
Consider this small test class: import java.util.List; public abstract class Test { // CAN
Given the following classes... public abstract class FooBase<TBar> where TBar : BarBase{} public abstract
I have an abstract class: class A { public: bool loaded_; virtual int load()

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.