在C#中初始化变量时出现错误

时间:2020-01-22 14:43:36

标签: c# variables scopes

using System;

namespace MergeSortProgram
{
    class MergeSort
    {
        // Merges two subarrays of arr[]. 
        // First subarray is arr[l..m] 
        // Second subarray is arr[m+1..r] 
        public  void merge(int [] arr, int l, int m, int r)
        {
            // Find sizes of two subarrays to be merged 
            int n1 = m - l + 1;
            int n2 = r - m;

            /* Create temp arrays */
            int []L = new int[n1];
            int []R = new int[n2];


            /*Copy data to temp arrays*/
            for (int i = 0; i < n1; ++i)
            {
                L[i] = arr[l + i];
            }
            for (int j = 0; j < n2; ++j)
            {
                R[j] = arr[m + 1 + j];
            }


            /* Merge the temp arrays */

            // Initial indexes of first and second subarrays 
            int i = 0, j = 0;

            // Initial index of merged subarry array 
            int k = l;
            while (i < n1 && j < n2)
            {
                if (L[i] <= R[j])
                {
                    arr[k] = L[i];
                    i++;
                }
                else
                {
                    arr[k] = R[j];
                    j++;
                }
                k++;
            }

            /* Copy remaining elements of L[] if any */
            while (i < n1)
            {
                arr[k] = L[i];
                i++;
                k++;
            }

            /* Copy remaining elements of R[] if any */
            while (j < n2)
            {
                arr[k] = R[j];
                j++;
                k++;
            }
        }

        // Main function that sorts arr[l..r] using 
        // merge() 
        public  void sort(int [] arr, int l, int r)
        {
            if (l < r)
            {
                // Find the middle point 
                int m = (l + r) / 2;

                // Sort first and second halves 
                sort(arr, l, m);
                sort(arr, m + 1, r);

                // Merge the sorted halves 
                merge(arr, l, m, r);
            }
        }

        /* A utility function to print array of size n */
        static void printArray(int [] arr)
        {
            int n = arr.Length;
            for (int i = 0; i < n; ++i)
                Console.Write(arr[i] + " ");
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            int []arr= { 12, 11, 13, 5, 6, 7 };

           Console.WriteLine("Given Array");
            printArray(arr);
            MergeSort ps = new MergeSort();
            ps.sort(arr, 0, arr.Length - 1);

            Console.WriteLine("\nSorted array");
            printArray(arr);
        }

    }
}

合并函数中for循环的变量i和j给出此错误。 不能在此范围内声明名为“ i”的本地或参数,因为该名称在封闭的本地范围内用于定义本地或参数

有帮助吗?

1 个答案:

答案 0 :(得分:4)

最终,它可以简化为:

for (int i = 0; i < 5; i++) { }
int i = 42;

(请参见i附近的j/*Copy data to temp arrays*/

对当地人的范围如此之大,以至于他们在这里不能共享相同的名字;因此,可以将某些代码移至另一种方法,或者更改一个本地名称,以便在不同的上下文中没有两个i / j

可替代地,但可以令人困惑地:

int i;
for (i = 0; i < 5; i++) { }
i = 42;