C # 4.0 Default Parameters

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); } } 

Exiting this program:

 I am the default value on the interface 

(1) Why is there no way to specify a parameter as optional on an interface without providing a value. I am considering the default value for detailing the implementation. If we wrote this code in the style of a pre-optional parameter, we would create two overloads in the interface, and the default value would be specified in the implementation class. That is, 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); } } 

What exits are expected

 I am the default value set on the implementing class 

(2) Why can't we override the default value in the implementation class!

+4
source share
2 answers

The default values ​​in .Net are compiler-based syntactic sugar. At the place of the call, the compiler adds 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.

Therefore, they cannot be “redefined” in implementations because there is nothing to redefine.

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

Update
From your comments, what you are suggesting is either some form of “virtual” parameter (in which the runtime type is declared) that the CLR should have “known” about. I assume that this implementation was excluded due to the fact that the costs (design, documentation, implementation, testing, etc.) were too high compared to the advantages that it gave (although this is only an assumption!). In addition, there is a default delegation option, for example:

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

Gets rewritten by the compiler as:

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

But descending this route leads to a potentially unacceptable level of congestion when several optional arguments and named arguments are accepted into the arguments.

+4
source

1) There is a flaw in your thinking. You say that the default is the implementation detail. Interfaces do not contain implementation details. By this logic, specifying a default value does not really belong to the interface.

Just write an interface using overloaded methods. Not only will the definition be more understandable, but the interface will be more compatible when interacting with other languages, and you will not have problems with versions that come with optional parameters.

2) Because the interface defines a work contract. The interface says that there should be something by default ... so the class should implement it that way.

+3
source

Source: https://habr.com/ru/post/1381154/


All Articles