在下面的方法中,从不使用参数 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/