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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:46:21+00:00 2026-05-28T13:46:21+00:00

Seems like in .NET Framework there is an issue with optional parameters when you

  • 0

Seems like in .NET Framework there is an issue with optional parameters when you override the method. The output of the code below is:
“bbb”
“aaa”
. But the output I’m expecting is:
“bbb”
“bbb”
.Is there a solution for this. I know it can be solved with method overloading but wondering the reason for this. Also the code works fine in Mono.

class Program
{
    class AAA
    {
        public virtual void MyMethod(string s = "aaa")
        {
            Console.WriteLine(s);
        }

        public virtual void MyMethod2()
        {
            MyMethod();
        }
    }

    class BBB : AAA
    {
        public override void MyMethod(string s = "bbb")
        {
            base.MyMethod(s);
        }

        public override void MyMethod2()
        {
            MyMethod();
        }
    }

    static void Main(string[] args)
    {
        BBB asd = new BBB();
        asd.MyMethod();
        asd.MyMethod2();
    }
}
  • 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-28T13:46:22+00:00Added an answer on May 28, 2026 at 1:46 pm

    One thing worth noting here, is that the overridden version is called each time. Change the override to:

    public override void MyMethod(string s = "bbb")
    {
      Console.Write("derived: ");
      base.MyMethod(s);
    }
    

    And the output is:

    derived: bbb
    derived: aaa
    

    A method in a class can do one or two of the following:

    1. It defines an interface for other code to call.
    2. It defines an implementation to execute when called.

    It may not do both, as an abstract method does only the former.

    Within BBB the call MyMethod() calls a method defined in AAA.

    Because there is an override in BBB, calling that method results in an implementation in BBB being called.

    Now, the definition in AAA informs calling code of two things (well, a few others too that don’t matter here).

    1. The signature void MyMethod(string).
    2. (For those languages that support it) the default value for the single parameter is "aaa" and therefore when compiling code of the form MyMethod() if no method matching MyMethod() can be found, you may replace it with a call to `MyMethod(“aaa”).

    So, that’s what the call in BBB does: The compiler sees a call to MyMethod(), doesn’t find a method MyMethod() but does find a method MyMethod(string). It also sees that at the place where it is defined there’s a default value of “aaa”, so at compile time it changes this to a call to MyMethod("aaa").

    From within BBB, AAA is considered the place where AAA‘s methods are defined, even if overridden in BBB, so that they can be over-ridden.

    At run-time, MyMethod(string) is called with the argument “aaa”. Because there is a overridden form, that is the form called, but it is not called with “bbb” because that value has nothing to do with the run-time implementation but with the compile-time definition.

    Adding this. changes which definition is examined, and so changes what argument is used in the call.

    Edit: Why this seems more intuitive to me.

    Personally, and since I’m talking of what is intuitive it can only be personal, I find this more intuitive for the following reason:

    If I was coding BBB then whether calling or overriding MyMethod(string), I’d think of that as “doing AAA stuff” – it’s BBBs take on “doing AAA stuff”, but it’s doing AAA stuff all the same. Hence whether calling or overriding, I’m going to be aware of the fact that it was AAA that defined MyMethod(string).

    If I was calling code that used BBB, I’d think of “using BBB stuff”. I might not be very aware of which was originally defined in AAA, and I’d perhaps think of this as merely an implementation detail (if I didn’t also use the AAA interface nearby).

    The compiler’s behaviour matches my intuition, which is why when first reading the question it seemed to me that Mono had a bug. Upon consideration, I can’t see how either fulfils the specified behaviour better than the other.

    For that matter though, while remaining at a personal level, I’d never use optional parameters with abstract, virtual or overridden methods, and if overriding someone else’s that did, I’d match theirs.

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

Sidebar

Related Questions

I'm learning ASP.NET MVC Framework, From some articles like this , it seems that
Not sure what exactly is going on here, but seems like in .NET 1.1
It seems like when you have a WinForms .NET application, and a ComboBox (set
Seems like the subtraction is triggering some kind of issue and the resulting value
I'd like to use the .NET settings designer/framework to create application settings at design
I have a windows service written in C# .NET framework 3.5 and would like
Does a C++ Windows Forms Application require .NET Framework? It seems that it does
Is there a way in the .NET framework to validate that a process has
There are a lot of post about rails-like database migrations in .NET, but none
Using Visual Studio 2010 .Net Framework 4 C# Linq to Entities Issue I would

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.