using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ch6_13
{
    public partial class Form1 : Form
    {
        // modify elements of array and attempt to modify reference
        public static void FirstDouble(int[] array)
        {
            // double each element's value                          
            for (int i = 0; i < array.Length; i++)
                array[i] *= 2;

            // create new object and assign its reference to array
            array = new int[] { 11, 12, 13 };
        } // end method FirstDouble                                
        // modify elements of array and change reference array
        // to refer to a new array                            
        public static void SecondDouble(ref int[] array)
        {
            // double each element's value                     
            for (int i = 0; i < array.Length; i++)
                array[i] *= 2;
            // create new object and assign its reference to array
            array = new int[] { 11, 12, 13 };
        } // end method SecondDouble                          

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // create and initialize firstArray
            int[] firstArray = { 1, 2, 3 };
            // copy the reference in variable firstArray
            int[] firstArrayCopy = firstArray;
            label1.Text = "Test passing firstArray reference by value";
            label1.Text += "\nContents of firstArray " +
                "before calling FirstDouble:\n\t";
            // print contents of firstArray
            for (int i = 0; i < firstArray.Length; i++)
                label1.Text += firstArray[i] + "   ";
            // pass variable firstArray by value to FirstDouble
            FirstDouble(firstArray);
            label1.Text += "\n\nContents of firstArray after " +
                           "calling FirstDouble\n\t";
            // print contents of firstArray
            for (int i = 0; i < firstArray.Length; i++)
                label1.Text += firstArray[i] + "   ";
            // test whether reference was changed by FirstDouble
            if (firstArray == firstArrayCopy)
                label1.Text += "\n\nThe references refer to different arrays";
            else
                label1.Text += "\n\nThe references refer to different arrays";
            // create and initialize secondArray
            int[] secondArray = { 1, 2, 3 };
            // copy the reference in variable secondArray
            int[] secondArrayCopy = secondArray;
            label1.Text += "\nTest passing secondArray " +
                           "reference by reference";
            label1.Text += "\nContents of secondArray " +
                      "before calling SecondDouble:\n\t";
            // print contents of secondArray before method call
            for (int i = 0; i < secondArray.Length; i++)
                label1.Text += secondArray[i] + "   ";
            // pass variable secondArray by reference to SecondDouble
            SecondDouble(ref secondArray);
            label1.Text += "\n\nContents of secondArray " +
                        "after calling SecondDouble:\n\t";
            // print contents of secondArray after method call
            for (int i = 0; i < secondArray.Length; i++)
                label1.Text += secondArray[i] + "   ";
            // test whether reference was changed by SecondDouble
            if (secondArray == secondArrayCopy)
                label1.Text += "\n\nThe references refer to the same array";
            else
                label1.Text += "\n\nThe references refer to different arrays";
        }
    }
}
