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

  • Home
  • SEARCH
  • 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 8711203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:48:59+00:00 2026-06-13T04:48:59+00:00

I’ve been using optional parameters in a limited fashion for some time now with

  • 0

I’ve been using optional parameters in a limited fashion for some time now with .NET 4. I’ve recently thought about how the optional parameters are implemented and something dawned on me.

Take the following example:

public interface ITest
{
    void Go(string val = "Interface");
}

The only requirement for implementing this is that the largest form of the signature void Go (string val) be implemented, in fact we need not implement the optional parameter because the compiler takes care of that wire-up (although the option to use optional parameters will not be available when using the concrete class directly).

With this said, to provide the functionality in both places, and for it to be discover-able in the implementation, one could implement the optional declaration in both interface AND implementation. In fact, the productivity tool ReSharper will pull this optional declaration up to the concrete type automatically when implementing an interface. This seems the logical thing to do. However…

What’s to stop us from using different values in the concrete type and interface? This set off my alarm bells this morning as if someone goes in to change that value, and forgets to persist it down the inheritance of overrides / interfaces, the behaviour will be completely different depending on how you access the object. This could be very frustrating to debug.

Try this LINQpad Script:

void Main()
{
    IA iface = new A();
    A cls = new A();

    iface.Go();
    cls.Go();
}

interface IA
{
    void Go(string val = "Interface");
}

class A : IA
{
    public void Go(string val = "Class") { val.Dump(); }
}

The output will be:

Interface
Class

Which brings me to my actual question:

Question:

What (if any?) way can we safeguard this, without losing the ability to use the optional parameter variant from the concrete class, and so it’s discoverable / readable to the coder?

Has anyone encountered this problem before? How did you solve it? Is there any best practices that could help prevent this problem from becoming prevalent in a multi-developer large-scale codebase?

  • 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-13T04:49:00+00:00Added an answer on June 13, 2026 at 4:49 am

    Nothing stops you from doing that. Using optional parameters bakes the value into the method call. So as you are seeing, calling IA.Go() and A.Go() give you different values because they’re compiled to IA.Go("Interface") and A.Go("Class").

    The way around it (meaning to only supply the “default” parameter in the implementation) is to have an overload instead of optional parameters:

    interface IA
    {
        void Go();
        void Go(string val);
    }
    
    public class A : IA
    {
        public void Go()
        {
            Go("Class");
        }
        public void Go(string val) { val.Dump(); }
    }
    

    Some other options:

    • Add a unit test that checks for that condition. You’d have to do it for every implementation, however.
    • Research creating a custom code analysis rule.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
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 just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.