.net - Overloaded methods give "Method with optional parameter is hidden by overload" warning in Resharper -


i have few c# apps logging, , output method has overload accept message , streamwriter, , overload additional parameter params array. example of 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 gives following warning these methods: "method optional parameter hidden overload".

the warning misleading because call 2-param overload inside 3 param overload , not result in recursive call, overload not hidden.

i did research on resharper site , there have been tickets opened on issue have been closed "will not fix".

it seems me valid use case, since runtime knows overload call. there examples in .net framework use such overloads.

for example, streamwriter.writeline() has overloads value write, , format params.

is valid argument, or should methods renamed "outputformat" since behind scenes using string.format build string specified params?

as far see, there 2 questions in post.

first of all, if feel methods renamed more obvious go ahead, improve code on many aspect (readability, usability, etc.) , should anyway describe close possible do.

second, resharper warning :

recursivity using overloaded functions not necessary implies or leads warning seeing.

you know overloaded function commonly used when parameters of function has different types, function same thing, such :

private static void print(int i) {...}  private static void print(bool b) {...} 

however, if function overloaded , if overload has exact same parameters type optional parameters, have design problem.

basic explanation

if have :

private static void print(string message) {...}  private static void print(string message, string messagedelimiter = "===\n") {...} 

when call print function class, since both function same way when call them : print("my message"); 1 optional parameter hidden.

thus, merged them :

private static void print(string message, string messagedelimiter = "===\n") {...} 

moreover

you might want more clever, giving user access 1 public function while restricting 1 optional parameter :

public static void print(string message) {...} //< can see 1 public  private static void print(string message, string messagedelimiter = "===\n") {...} 

even if case, encounter same problem.

imo, rule of thumb ask few questions :

  • does optional parameter make sense is?
  • does function needs hold same name?
  • does parameter should optional?

if answer yes of them, "ok" ignore resharper comment , let code is.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -