gpt4 book ai didi

c# - 我如何重构这两种方法?

转载 作者:太空宇宙 更新时间:2023-11-03 18:37:57 25 4
gpt4 key购买 nike

<分区>

    public static void outputDetail(DateTime previousTime, ref double[] array, StreamWriter streamWriter)  //the parameter in here is not necessary, but want to maintain a similiarity in the TimeOfDay class
{
string outputString = previousTime.ToString("yyyy/MM/dd");
Boolean bypass = true;

for (int i = 1; i < array.Length - 1; i++)
{
outputString = outputString + "," + array[i].ToString();

if (array[i] != 0)
bypass = false;
}
if (bypass == false)
streamWriter.WriteLine(outputString);

for (int i = 0; i < array.Length; i++)
{
array[i] = 0;
}
}


public static void outputDetail(DateTime previousTime, ref int[] array, StreamWriter streamWriter) //the parameter in here is not necessary, but want to maintain a similiarity in the TimeOfDay class
{
string outputString = previousTime.ToString("yyyy/MM/dd");
Boolean bypass = true;

for (int i = 1; i < array.Length -1; i++)
{
if (array[i] != 0)
{
outputString = outputString + "," + array[i].ToString();
bypass = false;
}
else
{
outputString = outputString + ",";
}
}
if (bypass == false)
streamWriter.WriteLine(outputString);

for (int i = 0; i < array.Length; i++)
{
array[i] = 0;
}
}

它们完全相同,只有一个采用 double 组,一个采用 int 数组,我看到一些示例使用 Iconvertible,但我无法获得正确的语法。有人可以为该方法发布一些可行的代码段吗?

我该怎么调用它?

编辑:非常感谢您的回答,我有另一个更复杂的案例需要重构,这里的建议不适用于这两种方法。请点击此链接了解更多详情

How do I refactor these 2 methods? Part 2.

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com