c# - Resharper 建议但不对此代码块执行任何重构

标签 c# asp.net resharper

在下面的方法中,从不使用参数 fromDate 和 toDate 的值,因为它们在读取之前在正文中被覆盖。

static void GetDatesFromMonth(string month, ref DateTime fromDate, ref DateTime toDate)
{
  DateTime userDateTime = TimeHelper.getUserGMTNow();
  DateTime calculatedDate = Convert.ToDateTime(month + " 01," + userDateTime.Year);
  toDate = calculatedDate.AddMonths(1).AddSeconds(-1);
  fromDate = toDate.AddMonths(-12).AddSeconds(1);
}

我在我的类文件中的许多地方都使用了这段代码。

当我在我的代码上运行 Resharper 时,它会显示此消息,并且与所有其他建议不同,它无法更正此代码块

谁能帮我用良好的编码习惯重写这个方法。

最佳答案

将两个日期参数改为out

static void GetDatesFromMonth(string month, out DateTime fromDate, out DateTime toDate) 

See here澄清 out vs ref

简单地说,当您的方法需要返回两个或更多值时,您会使用 out,out 的意思是“我将在退出前设置这些值”。相反 ref 更复杂。这意味着“我需要这个方法中的这个值/对象,我会在退出前更改它们”

关于c# - Resharper 建议但不对此代码块执行任何重构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9757504/

相关文章:

c# - 如何部署windows应用商店项目(Visual Studio Ultimate 2012)

c# - 在 cors 主机中设置时,是否需要在 C# 中定义协议(protocol)

c# - 使用 Visual Studio 2008/2010 或 Resharper 5 创建自动属性的快捷方式

jquery - 如何返回Web API数据

c# - Sitecore 项目的错误处理

resharper - MonoDevelop 是否有 "Resharper-like"插件?

c# - Resharper:在调试期间显示表达式值

c# - 在 AuthorizeAttribute Mvc Core Web Api 中获取 Controller 实例

c# - OnTriggerEnter() 未调用

c# - GC generation 3 出现在 windbg 中