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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:17:27+00:00 2026-05-26T21:17:27+00:00

Consider the following console application: class Program { static void Main() { MyInterface test

  • 0

Consider the following console application:

class Program
{
    static void Main()
    {
        MyInterface test = new MyClass();
        test.MyMethod();

        Console.ReadKey();
    }
}

interface MyInterface
{
    void MyMethod(string myString = "I am the default value on the interface");
}

class MyClass : MyInterface
{
    public void MyMethod(string myString = "I am the default value set on the implementing class")
    {
        Console.WriteLine(myString);
    }
}

The output from this program is:

I am the default value on the interface

(1) Why isn’t there a way of specifying a parameter as optional on an interface without supplying a value. I consider the default value to be implementation detail. If we wrote this code in the pre-optional parameter style we would create two overloads in the interface and the default value would be specified in the implementing class. I.e. we would have:

interface MyInterface
{
    void MyMethod();

    void MyMethod(string myString);
}

class MyClass : MyInterface
{
    public void MyMethod()
    {
        MyMethod("I am the default value set on the implementing class");
    }

    public void MyMethod(string myString)
    {
        Console.WriteLine(myString);
    }
}

Which outputs as we would expect,

I am the default value set on the implementing class

(2) Why can’t we override the default value in the implementing class!

  • 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-26T21:17:28+00:00Added an answer on May 26, 2026 at 9:17 pm

    Default values in .Net are really compiler based syntactic sugar. At the call site the compiler adds in the default values for you. It cannot know the runtime type of your object at compile time so it must insert the value defined in the interface.

    Hence, they cannot be ‘overridden’ in implementations because there is nothing to override.

    Eric Lippert wrote a very interesting series of blog posts on the subject of optional arguments, the first of which can be found here.

    Update
    From your comments, what you’re suggesting is either some form of ‘virtual’ parameter (in which the runtime type declares), which the CLR would have to ‘know’ about. I’m guessing that this implementation was ruled out because the costs (designing, documenting, implementing,testing etc) were too high compared to the benefits it gave (although this is only a guess!).
    Alternatively, there’s the default delegating method option ie:

    void M(bool y = false) { ... whatever ... }
    

    Gets re-written by the compiler as:

    void M() { M(false); }
    void M(bool y) { ... whatever ... }
    

    But going down this route leads to potentially unacceptable level of overloads once multiple optional arguments and named arguments are taken into considertaion.

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

Sidebar

Related Questions

Consider the following code: class Program { static void Main(string[] args) { try {
Consider the following code: class Program { void Foo<T>() { } static void Main(string[]
Consider the following code: class Program { static void Main(string[] args) { A a
Consider the following code: namespace ConsoleApplication1 { class Program { static void Main(string[] args)
Consider the following code: using System; namespace ConsoleApplication2 { class Program { static void
Consider following program: static void Main (string[] args) { int i; uint ui; i
Consider the following code: namespace DisposeTest { using System; class Program { static void
Consider the following C# code: class Program { static public void Print(string toPrint) {
Consider the following code: static void Main(string[] args) { using (MemoryStream memoryStream = new
Consider the following class public class PlanetKrypton { public static void CallSuperManforHelp(string helpMessage, params

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.