I recently encountered the following behaviour;
internal interface IOptionalParamTest
{
void PrintMessage(string message = "Hello");
}
class OptionalParamTest : IOptionalParamTest
{
public void PrintMessage(string message = "Goodbye")
{
Console.WriteLine(message);
}
}
internal class Program
{
static void Main()
{
IOptionalParamTest usingInterface = new OptionalParamTest();
usingInterface.PrintMessage(); // prints "Hello"
OptionalParamTest usingConcrete = new OptionalParamTest();
usingConcrete.PrintMessage();// prints "Goodbye"
}
}
My question is; why does the compiler not reject the implementation of PrintMessage with a different default value from that defined on the interface?