Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are C# 4 optional parameters defined on interface not enforced on implementing class?

I noticed that with the optional parameters in C# 4 if you specify an optional parameter on an interface you don,t have to make that parameter optional on any implementing class:

public interface MyInterface {     void TestMethod(bool flag = false); }  public class MyClass : MyInterface {     public void TestMethod(bool flag)     {         Console.WriteLine(flag);     } } 

and therefore:

var obj = new MyClass();         obj.TestMethod(); // compiler error  var obj2 = new MyClass() as MyInterface; obj2.TestMethod(); // prints false 

Does anyone know why optional parameters are designed to work this way?

On one hand I suppose the ability to override any default values specified on the interfaces is useful though to be honest I'm not sure if you should even be able to specify default values on the interface as that should be an implementation decision.

On the other hand, this disconnect means you can't always use the concrete class and the interface interchangeably. This of course, wouldn't be a problem if the default value is specified on the implementation, but then if you're exposing your concrete class as the interface (using some IOC framework to inject the concrete class for instance) then really there's no point having the default value as the caller will have to always provide it anyway.

like image 380
theburningmonk Avatar asked Feb 07 '11 14:02

theburningmonk


People also ask

Why are we using C?

C is highly portable and is used for scripting system applications which form a major part of Windows, UNIX, and Linux operating system. C is a general-purpose programming language and can efficiently work on enterprise applications, games, graphics, and applications requiring calculations, etc.

Are C and C+ the same?

As we know both C and C++ are programming languages and used for application development. The main difference between both these languages is C is a procedural programming language and does not support classes and objects, while C++ is a combination of both procedural and object-oriented programming languages.

Why is C used in C?

%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".

Why learn C?

Why Learn C? There are an awful lot of programming languages available right now -- everything from the extremely high level (such as Visual Basic) to the low level power of assembly, and a good variety of specialized options in between ( Perl, Ruby, and Python are good choices for many tasks).

Why do so many programmers use C?

Plus, with C, you get lots of strong opinions mixed with insights that you can understand. As a result of its age and its use as the language of system programming for Unix, C has become something of the lingua franca of programming. C is a great language for expressing common ideas in programming in a way that most people are comfortable with.

Why do drives start from c?

Why Not A Or B? Why Do Drives Start From C? The various logical drives in Windows are assigned a drive letter. Generally, the drive letter for the first logical drive is C followed by D, E, F. Well, A and B are also alphabets but Windows reserves these drive letters for some special purpose, which is floppy drives.

Why was C programming language used in Unix?

Eventually, C was developed during 1971-73, containing both high-level functionality and the detailed features required to program an operating system. Hence, many of the UNIX components including UNIX kernel itself were eventually rewritten in C. As a middle-level language, C combines the features of both high-level and low-level languages.


2 Answers

UPDATE: This question was the subject of my blog on May 12th 2011. Thanks for the great question!

Suppose you have an interface as you describe, and a hundred classes that implement it. Then you decide to make one of the parameters of one of the interface's methods optional. Are you suggesting that the right thing to do is for the compiler to force the developer to find every implementation of that interface method, and make the parameter optional as well?

Suppose we did that. Now suppose the developer did not have the source code for the implementation:


// in metadata: public class B  {      public void TestMethod(bool b) {} } 

// in source code interface MyInterface  {      void TestMethod(bool b = false);  } class D : B, MyInterface {} // Legal because D's base class has a public method  // that implements the interface method 

How is the author of D supposed to make this work? Are they required in your world to call up the author of B on the phone and ask them to please ship them a new version of B that makes the method have an optional parameter?

That's not going to fly. What if two people call up the author of B, and one of them wants the default to be true and one of them wants it to be false? What if the author of B simply refuses to play along?

Perhaps in that case they would be required to say:

class D : B, MyInterface  {     public new void TestMethod(bool b = false)     {         base.TestMethod(b);     } } 

The proposed feature seems to add a lot of inconvenience for the programmer with no corresponding increase in representative power. What's the compelling benefit of this feature which justifies the increased cost to the user?


UPDATE: In the comments below, supercat suggests a language feature that would genuinely add power to the language and enable some scenarios similar to the one described in this question. FYI, that feature -- default implementations of methods in interfaces -- will be added to C# 8.

like image 76
Eric Lippert Avatar answered Oct 21 '22 11:10

Eric Lippert


An optional parameter is just tagged with an attribute. This attribute tells the compiler to insert the default value for that parameter at the call-site.

The call obj2.TestMethod(); is replaced by obj2.TestMethod(false); when the C# code gets compiled to IL, and not at JIT-time.

So in a way it's always the caller providing the default value with optional parameters. This also has consequences on binary versioning: If you change the default value but don't recompile the calling code it will continue to use the old default value.

On the other hand, this disconnect means you can't always use the concrete class and the interface interchangeably.

You already can't do that if the interface method was implemented explicitly.

like image 32
CodesInChaos Avatar answered Oct 21 '22 11:10

CodesInChaos