[英]Is there any reason to declare optional parameters in an interface?
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 .正如Eric Lippert 解释的那样,您可以在接口方法中声明可选参数,但实现类不需要将参数声明为可选参数。 Conversely, you can declare a parameter as optional in an implementing class but not in the interface.
相反,您可以在实现 class 中将参数声明为可选参数,但不能在接口中声明。
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) { }
}
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.传递给
MyService1a
的第二个参数将为true
,作为接口中的默认参数。
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.调用者只知道某个实现的接口而不是具体类型是相当普遍的 - 所以如果您认为可选参数是一个好主意(这是有争议的),那么将它们放在接口上和在具体类型上一样有意义.
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:如果一个人正在设计一个接口,其方法
Foo
接受参数Bar
,并且 99%(但不是 100%)对Foo
的调用为Bar
传递零,则必须:
Option #3 seems most convenient to me when it's workable.选项#3 在可行时对我来说似乎最方便。
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.
换句话说,派生的 class 中的实现者可以根据需要使参数可选,可以使其成为必需等。 If it isn't optional, the derived classes must have it.
如果它不是可选的,则派生类必须具有它。
Your example above shows just that -- flexibility in the derived classes.您上面的示例就说明了这一点——派生类的灵活性。
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.当您在作为接口实例的 object 上调用方法时,编译器将替换接口上指定的默认值。
And when you call method on an object that is an instance of class, then compiler will replace default values specified on the class.当您在作为 class 实例的 object 上调用方法时,编译器将替换 class 上指定的默认值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.