I was playing with optional parameters to see how they would work with interfaces and I came across a strange warning. The setup I had was the following code:
public interface ITestInterface
{
void TestOptional(int a = 5, int b = 10, object c = null);
}
public class TestClass : ITestInterface
{
void ITestInterface.TestOptional(int a = 5, int b = 5, object c = null)
{
Console.Write("a=" + a + " b=" + b + " c=" + c);
}
}
The compiler gives me the following warnings:
- The default value specified for parameter 'a' will have no effect because it applies to a member that is used in contexts that do not allow optional arguments
- The default value specified for parameter 'b' will have no effect because it applies to a member that is used in contexts that do not allow optional arguments
- The default value specified for parameter 'c' will have no effect because it applies to a member that is used in contexts that do not allow optional arguments
If I run this with the following code:
class Program
{
static void Main(string[] args)
{
ITestInterface test = new TestClass();
test.TestOptional();
Console.ReadLine();
}
}
I get the output of "a=5 b=10 c=
" as I'd expect.
My question is what is warning for? What contexts is it referring to?