Was ist wirklich seltsam ist, dass der Wert, den Sie für den optionalen Parameter in der Schnittstelle setzen tatsächlich macht einen Unterschied. Ich nehme an, Sie müssen in Frage stellen, ob der Wert ein Interface-Detail oder ein Implementierungsdetail ist. Ich hätte letzteres gesagt, aber die Dinge verhalten sich wie das erste. Der folgende Code gibt zum Beispiel 1 0 2 5 3 7 aus.
// 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);
}
}
}
Welche interessant ist, ist, dass, wenn Ihre Schnittstelle einen Parameter optional die Klasse macht die Umsetzung nicht das gleiche zu tun hat:
// 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);
}
}
}
Was jedoch ein Fehler zu sein scheint, ist, dass, wenn Sie implementieren die Schnittstelle explizit Der Wert, den Sie in der Klasse für den optionalen Wert angeben, ist sinnlos. Wie könnten Sie im folgenden Beispiel den Wert 9 verwenden? Dies gibt nicht einmal eine Warnung beim Kompilieren.
// 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);
}
}
}
Martins Kommentare unten geben ein detailliertes Beispiel für die verschiedenen Fallstricke, und ich wünschte, der Compiler würde nicht übereinstimmende Standardargumente kennzeichnen. Das heißt, ich verwende das in meinem Code, weil es meine Absicht als Entwickler sowohl auf der Interface- als auch auf der Implementierungsebene für andere Entwickler anzeigt. –
Interessante verwandte Fragen: "Gibt es einen Grund, optionale Parameter in einer Schnittstelle zu deklarieren?" (Https: // stackoverflow.com/questions/6752762 /) ", und" [Warum sind C# 4 optionale Parameter auf der Schnittstelle nicht am implementierenden Klasse definiert?] (https://stackoverflow.com/questions/4922714/) ", der zweite mit einem interessanten [Antwort von Lippert] (https://stackoverflow.com/a/4923642/1028230). – ruffin