问题
You can declare optional parameters in an interface method but implementing classes are not required to declare the parameters as optional, as Eric Lippert explained. Conversely, you can declare a parameter as optional in an implementing class but not in the interface.
So is there any reason to declare optional parameters in an interface? If not, why is it allowed?
Examples:
public interface IService1
{
void MyMethod(string text, bool flag = false);
}
public class MyService1a : IService1
{
public void MyMethod(string text, bool flag) {}
}
public class MyService1b : IService1
{
public void MyMethod(string text, bool flag = true) { }
}
public interface IService2
{
void MyMethod(string text, bool flag);
}
public class MyService2b : IService2
{
public void MyMethod(string text, bool flag = false) { }
}
回答1:
Example:
public interface IService1
{
void MyMethod(string text, bool flag = true);
}
public class MyService1a : IService1
{
public void MyMethod(string text, bool flag) { }
}
Usage:
IService1 ser = new MyService1a();
ser.MyMethod("A");
2nd parameter passed to MyService1a
will be true
, as default parameter in interface.
回答2:
The reason for doing so would be to make it easier for callers to use when the compile-time type they have is just the interface:
public void Foo(IService1 service)
{
service.MyMethod("Text"); // Calls MyMethod("Text", false)
}
It's fairly common for a caller to only know about the interface something implements rather than the concrete type - so if you believe optional parameters are a good idea at all (it's controversial) it makes as much sense to have them on interfaces as on concrete types.
回答3:
If one is designing an interface with a method Foo
that takes parameter Bar
, and 99% (but not 100%) of calls to Foo
pass zero for Bar
, one must either:
- Include within the interface method overloads which do and do not include parameter `Bar`, thus requiring every implementation of that interface to include an extra method, but freeing callers of the need to specify it.
- Only include a method which includes `Bar`, saving implementers the cost of the extra method, but requiring an extra parameter to be included at every call site.
- Define the parameter as optional within the interface, thus making things more convenient for both implementers and consumers.
Option #3 seems most convenient to me when it's workable.
回答4:
It is useful in that the interface can declare them the way it wants, so you get the exact flexibility that you wanted when making the interface. In other words, an implementer in a derived class can make the parameter optional, can make it required, etc. as desired. If it isn't optional, the derived classes must have it.
Your example above shows just that -- flexibility in the derived classes.
回答5:
Interface designer's assumption of default parameters may be different then implementer's design.
Default parameters are simply expanded by compiler and parameters are replaced by actual default values.
When you call method on an object that is an instance of an interface, then compiler will replace default values specified on the interface.
And when you call method on an object that is an instance of class, then compiler will replace default values specified on the class.
来源:https://stackoverflow.com/questions/24573153/optional-parameters-in-method-and-dependency-injection-in-class-and-in-interfa