简体   繁体   English

是否有任何理由在接口中声明可选参数?

[英]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传递零,则必须:

  1. 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.在接口方法重载中包含是否包含参数“Bar”,因此要求该接口的每个实现都包含一个额外的方法,但调用者无需指定它。
  2. 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.只包含一个包含 `Bar` 的方法,为实现者节省额外方法的成本,但需要在每个调用站点包含一个额外的参数。
  3. 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.选项#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.

相关问题 有什么理由不在存储过程中使用可选参数吗? - Is there any reason not to use optional parameters in a stored procedure? 有没有理由C#不支持手动内联方法? 可选参数怎么样? - Is there any reason C# does not support manual inline methods? And what about optional parameters? 显式实现带有可选参数的接口的警告 - Warning From Explicitly Implementing an Interface with Optional Parameters 基于接口的设计和可选的方法参数 - Interface based design and optional method parameters 接口中的可选参数没有任何默认值 - Optional arguments in interface without any default value 如何在接口中使用默认参数声明委托给函数? - How can I declare delegate to a function with default parameters in an interface? 在存储库接口中使用可选参数会被视为糟糕的设计吗? - Would using optional parameters in a repository interface be considered bad design? 在为WCF实现接口时,不能使用可选参数 - Can't use optional parameters when implementing an interface for a WCF 为什么在接口上定义的 C# 4 可选参数没有在实现类上强制执行? - Why are C# 4 optional parameters defined on interface not enforced on implementing class? 有没有理由喜欢模拟一个接口而不是一个具有可覆盖成员的类? - Is there any reason to prefer mocking an interface rather than a class with overridable members?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM