如何将方法名称作为参数传递?

时间:2017-05-22 18:29:20

标签: c# methods refactoring

我正在寻找一种方法来缩短我的代码。由于我的代码的很大一部分是可重复的,有没有办法将子方法名称作为参数传递?

这是让我的问题更加明确的一个更重要的部分:

Random rnd = new Random(Guid.NewGuid().GetHashCode());
        int[] ArrayRandom = new int[200000];
        for (int j = 0; j < ArrayRandom.Length; j++) ArrayRandom[j] = rnd.Next(int.MaxValue);

        Console.WriteLine("\nInsertionSort\nARRAY SIZE:\t TIME [ms]:");
        for (int u = 50000; u <= 200000; u += 10000)
        {
            int[] TestArray = new int[u];
            Array.Copy(ArrayRandom, TestArray, u);
            double ElapsedSeconds;
            long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
            for (int n = 0; n < (NIter + 1 + 1); ++n)  
            {
                long StartingTime = Stopwatch.GetTimestamp();
                InsertionSort(TestArray);
                long EndingTime = Stopwatch.GetTimestamp();
                IterationElapsedTime = EndingTime - StartingTime;
                ElapsedTime += IterationElapsedTime;
                if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
                if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
            }
            ElapsedTime -= (MinTime + MaxTime);
            ElapsedSeconds = ElapsedTime * (1000.0 / (NIter * Stopwatch.Frequency));
            Console.WriteLine("{0,-12}\t{1}", u, ElapsedSeconds.ToString("F4"));
        }

        Console.WriteLine("\nSelectionSort\nARRAY SIZE:\t TIME [ms]:");
        for (int u = 50000; u <= 200000; u += 10000)
        {
            int[] TestArray = new int[u];
            Array.Copy(ArrayRandom, TestArray, u);

            double ElapsedSeconds;
            long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
            for (int n = 0; n < (NIter + 1 + 1); ++n)  
            {
                long StartingTime = Stopwatch.GetTimestamp();
                SelectionSort(TestArray);
                long EndingTime = Stopwatch.GetTimestamp();
                IterationElapsedTime = EndingTime - StartingTime;
                ElapsedTime += IterationElapsedTime;
                if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
                if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
            }
            ElapsedTime -= (MinTime + MaxTime);
            ElapsedSeconds = ElapsedTime * (1000.0 / (NIter * Stopwatch.Frequency));
            Console.WriteLine("{0,-12}\t{1}", u, ElapsedSeconds.ToString("F4"));
        }
        Console.WriteLine("\nCoctailSort\nARRAY SIZE:\t TIME [ms]:");
        for (int u = 50000; u <= 200000; u += 10000)
        {
            int[] TestArray = new int[u];
            Array.Copy(ArrayRandom, TestArray, u);

            double ElapsedSeconds;
            long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
            for (int n = 0; n < (NIter + 1 + 1); ++n)  
            {
                long StartingTime = Stopwatch.GetTimestamp();
                CocktailSort(TestArray);
                long EndingTime = Stopwatch.GetTimestamp();
                IterationElapsedTime = EndingTime - StartingTime;
                ElapsedTime += IterationElapsedTime;
                if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
                if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
            }
            ElapsedTime -= (MinTime + MaxTime);
            ElapsedSeconds = ElapsedTime * (1000.0 / (NIter * Stopwatch.Frequency));
            Console.WriteLine("{0,-12}\t{1}", u, ElapsedSeconds.ToString("F4"));
        }
        Console.WriteLine("\nHeapSort\nARRAY SIZE:\t TIME [ms]:");
        for (int u = 50000; u <= 200000; u += 10000)
        {
            int[] TestArray = new int[u];
            Array.Copy(ArrayRandom, TestArray, u);

            double ElapsedSeconds;
            long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
            for (int n = 0; n < (NIter + 1 + 1); ++n)  
            {
                long StartingTime = Stopwatch.GetTimestamp();
                HeapSort(TestArray);
                long EndingTime = Stopwatch.GetTimestamp();
                IterationElapsedTime = EndingTime - StartingTime;
                ElapsedTime += IterationElapsedTime;
                if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
                if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
            }
            ElapsedTime -= (MinTime + MaxTime);
            ElapsedSeconds = ElapsedTime * (1000.0 / (NIter * Stopwatch.Frequency));
            Console.WriteLine("{0,-12}\t{1}", u, ElapsedSeconds.ToString("F4"));
        }

如您所见,正在改变的是排序方法名称。基本上我想改变,这个:

Console.WriteLine("\nInsertionSort\nARRAY SIZE:\t TIME [ms]:");
    for (int u = 50000; u <= 200000; u += 10000)
    {
        int[] TestArray = new int[u];
        Array.Copy(ArrayRandom, TestArray, u);
        double ElapsedSeconds;
        long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
        for (int n = 0; n < (NIter + 1 + 1); ++n)  
        {
            long StartingTime = Stopwatch.GetTimestamp();
            InsertionSort(TestArray);
            long EndingTime = Stopwatch.GetTimestamp();
            IterationElapsedTime = EndingTime - StartingTime;
            ElapsedTime += IterationElapsedTime;
            if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
            if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
        }
        ElapsedTime -= (MinTime + MaxTime);
        ElapsedSeconds = ElapsedTime * (1000.0 / (NIter * Stopwatch.Frequency));
        Console.WriteLine("{0,-12}\t{1}", u, ElapsedSeconds.ToString("F4"));
    }

这样的事情:

MethotImLookingFor(AnySort(TestArray));

2 个答案:

答案 0 :(得分:0)

如果你试图传递对sort方法的引用而不是硬编码,那么是的,你可以让这个方法接受你的sort方法的委托(函数指针):

private static void TimeMySortAlgorithm(Action<int[]> sortMethod)
{
    for (int u = 50000; u <= 200000; u += 10000)
    {
        int[] TestArray = new int[u];

        // Rest of the code

        // Invoke the delegate.
        sortMethod(TestArray);

        // Rest of the code
    }
}

假设您有InsertionSort这样的方法:

private static void InsertionSort(int[] array) { }

您可以按以下方式致电TimeMySortAlgorithm

TimeMySortAlgorithm(InsertionSort);
TimeMySortAlgorithm(CocktailSort);
TimeMySortAlgorithm(HeapSort);

详细了解delegates here

答案 1 :(得分:0)

将重复的代码重构为采用Action<int[]>参数的方法。

像这样(未经测试):

void Main()
{
    Random rnd = new Random(Guid.NewGuid().GetHashCode());
    int[] ArrayRandom = new int[200000];
    for (int j = 0; j < ArrayRandom.Length; j++) ArrayRandom[j] = rnd.Next(int.MaxValue);

    performSort("Heap Sort", ArrayRandom, HeapSort);
    performSort("Cocktail Sort", ArrayRandom, HeapSort);
    performSort("Selection Sort", ArrayRandom, HeapSort);
    performSort("Insertion Sort", ArrayRandom, HeapSort);
}

public void performSort(string sortName, int[] arrayToSort, Action<int[]> sortFunction)
{
    int[] ArrayRandom = arrayToSort;

    Console.WriteLine("\n{0}\nARRAY SIZE:\t TIME [ms]:", sortName);
    for (int u = 50000; u <= 200000; u += 10000)
    {
        int[] TestArray = new int[u];
        Array.Copy(ArrayRandom, TestArray, u);
        double ElapsedSeconds;
        long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
        for (int n = 0; n < (NIter + 1 + 1); ++n)
        {
            long StartingTime = Stopwatch.GetTimestamp();
            sortFunction.Invoke(TestArray);
            long EndingTime = Stopwatch.GetTimestamp();
            IterationElapsedTime = EndingTime - StartingTime;
            ElapsedTime += IterationElapsedTime;
            if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
            if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
        }
        ElapsedTime -= (MinTime + MaxTime);
        ElapsedSeconds = ElapsedTime * (1000.0 / (NIter * Stopwatch.Frequency));
        Console.WriteLine("{0,-12}\t{1}", u, ElapsedSeconds.ToString("F4"));
    }
}

public void HeapSort(int[] array) { //Sorting code here }
public void CocktailSort(int[] array) { //Sorting code here }
public void InsertionSort(int[] array) { //Sorting code here }
public void SelectionSort(int[] array) { //Sorting code here }