22

You can declare optional parameters in an interface method but implementing classes are not required to declare the parameters as optional, as Eric Lippert explained. Conversely, you can declare a parameter as optional in an implementing class but not in the interface.

So is there any reason to declare optional parameters in an interface? If not, why is it allowed?

Examples:

public interface IService1
{
    void MyMethod(string text, bool flag = false);
}

public class MyService1a : IService1
{
    public void MyMethod(string text, bool flag) {}
}

public class MyService1b : IService1
{
    public void MyMethod(string text, bool flag = true) { }
}

public interface IService2
{
    void MyMethod(string text, bool flag);
}

public class MyService2b : IService2
{
    public void MyMethod(string text, bool flag = false) { }
}
1

6 Answers 6

23

Example:

public interface IService1
{
    void MyMethod(string text, bool flag = true);
}

public class MyService1a : IService1
{
    public void MyMethod(string text, bool flag) { }
}

Usage:

IService1 ser = new MyService1a();
ser.MyMethod("A");

2nd parameter passed to MyService1a will be true, as default parameter in interface.

21

The reason for doing so would be to make it easier for callers to use when the compile-time type they have is just the interface:

public void Foo(IService1 service)
{
    service.MyMethod("Text"); // Calls MyMethod("Text", false)
}

It's fairly common for a caller to only know about the interface something implements rather than the concrete type - so if you believe optional parameters are a good idea at all (it's controversial) it makes as much sense to have them on interfaces as on concrete types.

3

If one is designing an interface with a method Foo that takes parameter Bar, and 99% (but not 100%) of calls to Foo pass zero for Bar, one must either:

  1. Include within the interface method overloads which do and do not include parameter `Bar`, thus requiring every implementation of that interface to include an extra method, but freeing callers of the need to specify it.
  2. Only include a method which includes `Bar`, saving implementers the cost of the extra method, but requiring an extra parameter to be included at every call site.
  3. Define the parameter as optional within the interface, thus making things more convenient for both implementers and consumers.

Option #3 seems most convenient to me when it's workable.

1

It is useful in that the interface can declare them the way it wants, so you get the exact flexibility that you wanted when making the interface. In other words, an implementer in a derived class can make the parameter optional, can make it required, etc. as desired. If it isn't optional, the derived classes must have it.

Your example above shows just that -- flexibility in the derived classes.

0

Interface designer's assumption of default parameters may be different then implementer's design.

Default parameters are simply expanded by compiler and parameters are replaced by actual default values.

When you call method on an object that is an instance of an interface, then compiler will replace default values specified on the interface.

And when you call method on an object that is an instance of class, then compiler will replace default values specified on the class.

0

I don't think you should mainly because interfaces are not meant to handle matters of state but matters of function. Classes themselves are supposed to handle matters of state and defining properties as well as what is done by those classes (I consider an optional parameter a subtle definition of state). The catch is on the declaration and use of an object by appealing to its interface or its class. Example:

public interface IFoo
{
    void Bar(int i, int j);
}

public class Foo : IFoo
{
    public void Bar(int i, int j = 0)
    {
        Console.WriteLine(i);
        Console.WriteLine(j);
    }
}

class Program
{
    static void Main(string[] args)
    {
        //referring to interface requires second arg
        IFoo foo = new Foo();
        foo.Bar(1);

        //allows one arg and uses optional arg
        Foo foo = new Foo();
        foo.Bar(1);
    }
}

Essentially the functionality resorts to the type the object is encapsulated in with the corresponding requirements for that class or interface applying on the object. If the interface requires it also requires (for it is functioning like the interface) where if the interface did NOT require it it would ALSO not require it (functioning like the interface). You have to ask yourself though whether the interface or implementing/inheriting objects are structured correctly, but then again the flexibility of having a default value for the interface is not a hard and fast rule against.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.