Question

I have a few C# apps that do logging, and the Output method has an overload to accept the message and a StreamWriter, and another overload with an additional parameter for a params array. An example of the method signatures is:

private static void Output(string message, StreamWriter writer, params object[] args) 
{..}

private static void Output(string message, StreamWriter writer) 
{..}

The question concerns Resharper that gives the following warning for these methods: "Method with optional parameter is hidden by overload".

The warning is misleading because I call the 2-param overload from inside the 3 param overload and it does not result in a recursive call, so the overload is not hidden.

I did some research on the Resharper site and there have been some tickets opened on this issue that have been closed as "will not fix".

It seems to me that this is a valid use case, since the runtime knows which overload to call. Also there are examples in the .NET framework where they use such overloads.

For example, StreamWriter.WriteLine() has overloads for the value to write, and also Format params.

Is this a valid argument, or should my methods be renamed to something like "OutputFormat" since behind the scenes they are using string.Format to build a string with the specified params?

Was it helpful?

Solution

As far as I see, there are two questions in your post.

First of all, if you feel that your methods could be renamed into something more obvious go ahead, that will improve your code on many aspect (readability, usability, etc.) and they should anyway describe as close as possible what they do.

Second, about the Resharper warning :

Recursivity using overloaded functions does not necessary implies or leads to the warning you are seeing.

You probably know that overloaded function is most commonly used when parameters of a function has different types, but the function does the same thing, such as :

private static void Print(int i) {...}

private static void Print(bool b) {...}

However, if a function is overloaded and if that overload has the exact same parameters type as well as optional parameters, you most likely have a design problem.

Basic Explanation

If you have something like this :

private static void Print(string message) {...}

private static void Print(string message, string messageDelimiter = "===\n") {...}

When you will call the Print function from your class, since both function will look the same way when you call them : Print("my message"); the one with the optional parameter is hidden.

Thus, you could simply merged them like that :

private static void Print(string message, string messageDelimiter = "===\n") {...}

Moreover

You also might want to do something more clever, like giving the user access to one public function while restricting the one with the optional parameter like that :

public static void Print(string message) {...} //< As you can see this one is public

private static void Print(string message, string messageDelimiter = "===\n") {...}

Even if that case, You will encounter the same problem.

IMO, a good rule of thumb is to ask yourself few questions :

  • Does the optional parameter really make sense where it is?
  • Does the function really needs to hold the same name?
  • Does the parameter should really be optional?

If you answer yes to all of them, it could be "ok" to ignore Resharper comment and let your code as it is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top