重载方法在Resharper中给出“带有可选参数的方法被重载隐藏”警告

时间:2022-03-21 10:56:10

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:

我有几个C#应用程序进行日志记录,并且Output方法有一个重载来接受消息和一个StreamWriter,另一个重载带有一个params数组的附加参数。方法签名的一个示例是:

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".

问题涉及Resharper对这些方法给出以下警告:“带有可选参数的方法被过载隐藏”。

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.

该警告具有误导性,因为我从3个param重载内部调用2-param重载并且它不会导致递归调用,因此不会隐藏重载。

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".

我对Resharper网站进行了一些研究,并且已经在这个问题上打开了一些已经关闭的门票,因为“将无法修复”。

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.

在我看来,这是一个有效的用例,因为运行时知道调用哪个重载。 .NET框架中也有一些例子,他们使用这种重载。

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

例如,StreamWriter.WriteLine()具有要写入的值的重载,以及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?

这是一个有效的参数,还是应该将我的方法重命名为类似“OutputFormat”的东西,因为它们在幕后使用string.Format来构建一个带有指定参数的字符串?

1 个解决方案

#1


19  

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 :

第二,关于Resharper警告:

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 :

IMO,一个很好的经验法则是问自己几个问题:

  • 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.

如果你对所有人都回答“是”,那么忽略Resharper评论并让你的代码保持原样可能“没问题”。

#1


19  

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 :

第二,关于Resharper警告:

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 :

IMO,一个很好的经验法则是问自己几个问题:

  • 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.

如果你对所有人都回答“是”,那么忽略Resharper评论并让你的代码保持原样可能“没问题”。