Recently I have come across code that had been refactored using default parameters in C#. The refactoring had taken place in a DLL and it caused an interesting problem in the calling client code. When I looked at the code at first I could not understand how the refactoring had broken the client code so I experimented with this code to reproduce the problem.

The original code looked something like this.

namespace Library
{
    public class Utility
    {
        public string TestMethod()
        {
            return "original";
        }
    }
}

The client code like this

namespace CallError
{
    class Program
    {
        static void Main(string[] args)
        {
            var util = new Library.Utility();
            Console.WriteLine(util.TestMethod());
            Console.Read();
        }
    }
}

This was refactored to this

namespace Library
{
    public class Utility
    {
        public string TestMethod(System.Uri uri = null)
        {
            return "new";
        }
    }
}

Now I thought this would just work, After all there is no need to change the client code as the missing parameter will be defaulted. However when the client code was executed against the new Library DLL I got the following error.

Unhandled Exception: System.MissingMethodException: 
Method not found: 'System.String Library.Utility.TestMethod()'. at CallError.Program.Main(String[] args)

However when I recompiled the CallError client code it ran fine. Then when I though about it there error made sense: even with a default parameter the method signature has actually changed.

It made me think that care need to be taken when using default parameters in an external api interface.