我如何重构这两种方法? [英] how do i refactor these 2 methods?

查看:32
本文介绍了我如何重构这两种方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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;
        }
    }

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

they are exactly the same, only one takes a double array and one takes an int array, i see some example use Iconvertible but i cant get the syntax right. can someone post some workable snippet for the method pls?

我该怎么称呼它?

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

thanks very much for the answer, i have another somewhat more complicated case i need to refactor, and the suggestion in here dont work on that 2 methods. Please click this link for more detail

如何重构这两种方法?第 2 部分.

推荐答案

将您的代码更改为:

    public static void outputDetail<T>(DateTime previousTime, ref T[] array, System.IO.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 (!Object.Equals(array[i], default(T)))
            {
                outputString = outputString + "," + array[i].ToString();
                bypass = false;
            }
            else
            {
                outputString = outputString + ",";
            }
        }
        if (bypass == false)
            streamWriter.WriteLine(outputString);

        Array.Clear(array, 0, array.Length);
    }

有什么变化?

首先是方法签名:它接受一个 T 类型的泛型数组(所以它是否是 intdouble 并不重要,boolstrings).

First the method signature: it accepts a generic array of type T (so it doesn't matter if it's int, double, bool or strings).

现在您必须将比较修复为零.零是 int 和 double 的默认值,因此您可以使用 default(T) 获取实际类型的默认值,并使用 Object.Equals() 进行比较(==!= 运算符不是为泛型类型定义的.

Now you have to fix the comparison for zero. Zero is the default value for both int and double so you can use default(T) to get the default value for actual type and Object.Equals() for comparison (== and != operators aren't defined for generic types).

最后你只需要清除数组(再次用零),这样你就可以简单地使用 Array.Clear() 来完成所有的工作(它甚至比手工制作的 for).

Finally you just have to clear the array (again with zero) so you can simply use Array.Clear() to do all the job (and it's even little bit faster than a handmade for).

这篇关于我如何重构这两种方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆