views:

330

answers:

2

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?

+4  A: 

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.

pdr
But...it's new! And shiny! :-)
bryanjonker
A: 

Looks aside, that will do exactly what it sounds like you want to accomplish.

MojoFilter