对于接口的可选参数 [英] Optional parameters for interfaces
问题描述
使用C#4.0 - 建立一个接口和实现接口的类。我要声明的接口的可选参数,并将它在课堂上得到体现。所以,我有以下几点:
Using c# 4.0 -- building an interface and a class that implements the interface. I want to declare an optional parameter in the interface and have it be reflected in the class. So, I have the following:
public interface IFoo
{
void Bar(int i, int j=0);
}
public class Foo
{
void Bar(int i, int j=0) { // do stuff }
}
这编译,但它看起来不正确。接口需要有可选参数,否则它不会正确的接口方法签名反映。
This compiles, but it doesn't look right. The interface needs to have the optional parameters, because otherwise it doesn't reflect correctly in the interface method signature.
我应该跳过可选参数,只使用可空类型? ?还是将这项工作作为目的,没有副作用或后果
Should I skip the optional parameter and just use a nullable type? Or will this work as intended with no side effects or consequences?
推荐答案
您可以考虑预先可选参数的选择:
You could consider the pre-optional-parameters alternative:
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);
}
}
如果你不喜欢新的外观语言功能,你不必使用它。
If you don't like the look of a new language feature, you don't have to use it.
这篇关于对于接口的可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!