تمرير الصفائف باستخدام ref و out ( ارشادات برمجة C# )

مثل الكل المعلمات الخروج المعلمات out, يجب أن يتم تعيين معلمة نوع الصفيف قبل استخدامه; أي، يجب تعيين من قبل المستدعي. فعلى سبيل المثال:

static void TestMethod1(out int[] arr)
{
    arr = new int[10];   // definite assignment of arr
}

مثل الكل المعلمات ref ref يجب تعيين معلمة نوع الصفيف بواسطة المستدعي. لذلك، ليس هناك حاجة ليتم تعيينها من قبل المستدعي. المعلمة من نوع الصفيف ref ربما يكون نتيجة لتبديل هذا الاتصال. على سبيل المثال، الصفيف يمكن تعيين القيمة قيمة خالية أو يمكن تهيئته إلى صفيف مختلف. فعلى سبيل المثال:

static void TestMethod2(ref int[] arr)
{
    arr = new int[10];   // arr initialized to a different array
}

توضح الأمثلة التالية الفرق بين out و ref عند استخدامه في تمرير الصفائف إلى الأساليب.

مثال

في هذا المثال، يم تعريف الصفيف theArray في المستدعي ( الأسلوب Main) ، و يمت تهيئته في الأسلوب FillArray. ثم، عناصر الصفيف يتم إرجاعها إلى المستدعي وعرضها.

class TestOut
{
    static void FillArray(out int[] arr)
    {
        // Initialize the array:
        arr = new int[5] { 1, 2, 3, 4, 5 };
    }

    static void Main()
    {
        int[] theArray; // Initialization is not required

        // Pass the array to the callee using out:
        FillArray(out theArray);

        // Display the array elements:
        System.Console.WriteLine("Array elements are:");
        for (int i = 0; i < theArray.Length; i++)
        {
            System.Console.Write(theArray[i] + " ");
        }

        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}
    /* Output:
        Array elements are:
        1 2 3 4 5        
    */

في هذا المثال، تتم تهيئة الصفيف theArray في المستدعي ( Main أسلوب) ، و تمريره للأسلوب FillArray باستخدام المعلمة ref . يتم تحديث بعض عناصر الصفيف في الأسلوب FillArray. ثم، عناصر الصفيف يتم إرجاعها إلى المستدعي وعرضها.

class TestRef
{
    static void FillArray(ref int[] arr)
    {
        // Create the array on demand:
        if (arr == null)
        {
            arr = new int[10];
        }
        // Fill the array:
        arr[0] = 1111;
        arr[4] = 5555;
    }

    static void Main()
    {
        // Initialize the array:
        int[] theArray = { 1, 2, 3, 4, 5 };

        // Pass the array using ref:
        FillArray(ref theArray);

        // Display the updated array:
        System.Console.WriteLine("Array elements are:");
        for (int i = 0; i < theArray.Length; i++)
        {
            System.Console.Write(theArray[i] + " ");
        }

        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}
    /* Output:
        Array elements are:
        1111 2 3 4 5555
    */

راجع أيضًا:

المرجع

الصفائف (دليل البرمجة لـ #C)

الصفائف أحادية الأبعاد ( ارشادات برمجة C# )

الصفيف متعدد الأبعاد ( ارشادات برمجة C# )

الصفائف المزدحمة (إرشادات البرمجة لـ C#)

المبادئ

دليل البرمجة لـ #C