bry*_*ker 66 c# interface optional-parameters c#-4.0
使用c#4.0 - 构建一个接口和一个实现接口的类.我想在界面中声明一个可选参数,并让它反映在类中.所以,我有以下内容:
public interface IFoo
{
void Bar(int i, int j=0);
}
public class Foo
{
void Bar(int i, int j=0) { // do stuff }
}
Run Code Online (Sandbox Code Playgroud)
编译,但看起来不正确.接口需要具有可选参数,否则它在接口方法签名中无法正确反映.
我应该跳过可选参数并使用可空类型吗?或者这是否按预期工作,没有副作用或后果?
Mar*_*own 58
真正奇怪的是,您为接口中的可选参数赋予的值实际上有所不同.我想你必须质疑值是接口细节还是实现细节.我会说后者,但事情就像前者一样.例如,以下代码输出1 0 2 5 3 7.
// Output:
// 1 0
// 2 5
// 3 7
namespace ScrapCSConsole
{
using System;
interface IMyTest
{
void MyTestMethod(int notOptional, int optional = 5);
}
interface IMyOtherTest
{
void MyTestMethod(int notOptional, int optional = 7);
}
class MyTest : IMyTest, IMyOtherTest
{
public void MyTestMethod(int notOptional, int optional = 0)
{
Console.WriteLine(string.Format("{0} {1}", notOptional, optional));
}
}
class Program
{
static void Main(string[] args)
{
MyTest myTest1 = new MyTest();
myTest1.MyTestMethod(1);
IMyTest myTest2 = myTest1;
myTest2.MyTestMethod(2);
IMyOtherTest myTest3 = myTest1;
myTest3.MyTestMethod(3);
}
}
}
Run Code Online (Sandbox Code Playgroud)
有趣的是,如果你的接口使一个参数成为可选的,那么实现它的类不必做同样的事情:
// Optput:
// 2 5
namespace ScrapCSConsole
{
using System;
interface IMyTest
{
void MyTestMethod(int notOptional, int optional = 5);
}
class MyTest : IMyTest
{
public void MyTestMethod(int notOptional, int optional)
{
Console.WriteLine(string.Format("{0} {1}", notOptional, optional));
}
}
class Program
{
static void Main(string[] args)
{
MyTest myTest1 = new MyTest();
// The following line won't compile as it does not pass a required
// parameter.
//myTest1.MyTestMethod(1);
IMyTest myTest2 = myTest1;
myTest2.MyTestMethod(2);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然而,似乎是一个错误的是,如果你明确地实现了接口,那么你在类中为可选值赋予的值是没有意义的.在以下示例中,您如何使用值9?编译时甚至不会发出警告.
// Optput:
// 2 5
namespace ScrapCSConsole
{
using System;
interface IMyTest
{
void MyTestMethod(int notOptional, int optional = 5);
}
class MyTest : IMyTest
{
void IMyTest.MyTestMethod(int notOptional, int optional = 9)
{
Console.WriteLine(string.Format("{0} {1}", notOptional, optional));
}
}
class Program
{
static void Main(string[] args)
{
MyTest myTest1 = new MyTest();
// The following line won't compile as MyTest method is not available
// without first casting to IMyTest
//myTest1.MyTestMethod(1);
IMyTest myTest2 = new MyTest();
myTest2.MyTestMethod(2);
}
}
}
Run Code Online (Sandbox Code Playgroud)
pdr*_*pdr 29
您可以考虑pre-optional-parameters备选方案:
public interface IFoo
{
void Bar(int i, int j);
}
public static class FooOptionalExtensions
{
public static void Bar(this IFoo foo, int i)
{
foo.Bar(i, 0);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您不喜欢新语言功能的外观,则不必使用它.
You don't have to make the parameter optional in the implementation. Your code will make somewhat more sense then:
public interface IFoo
{
void Bar(int i, int j = 0);
}
public class Foo
{
void Bar(int i, int j) { // do stuff }
}
Run Code Online (Sandbox Code Playgroud)
This way, it's unambiguous what the default value is. In fact, I'm pretty sure the default value in the implementation will have no effect, since the interface provides a default for it.
| 归档时间: |
|
| 查看次数: |
46396 次 |
| 最近记录: |