Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 4.0 Default Parameters

Tags:

c#

c#-4.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!

like image 914
magritte Avatar asked Apr 02 '26 16:04

magritte


1 Answers

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.

like image 138
Rich O'Kelly Avatar answered Apr 04 '26 06:04

Rich O'Kelly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!