【发布时间】:2011-10-08 19:51:34
【问题描述】:
您可以在接口方法中声明可选参数,但实现类不需要将参数声明为可选参数,如Eric Lippert explained。相反,您可以在实现类中将参数声明为可选,但不能在接口中声明。
那么有什么理由在接口中声明可选参数吗?如果不是,为什么允许?
例子:
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) { }
}
【问题讨论】:
标签: c# c#-4.0 optional-parameters