ASP.NET Ref和Out关键字区别分析(2)

; this.button1.TabIndex = 0;
   this.button1.Text = "显示输出";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(8, 48);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(456, 336);
   this.label1.TabIndex = 1;
   this.label1.Text = "label1";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(480, 405);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.button1);
   this.MaximizeBox = false;
   this.MinimizeBox = false;
   this.Name = "Form1";
   this.Text = "Ref & Out";
   this.ResumeLayout(false);

}
  #endregion

/// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

private void button1_Click(object sender, System.EventArgs e)
  {
   int[] firstArray = {1, 2, 3};
   int[] firstArrayCopy = firstArray;

this.label1.Text = "Test Passing firstArray reference by value";
   this.label1.Text += "\n\nContents of firstArray before calling FirstDouble:\n\t";

   for(int i = 0;i < firstArray.Length; i++)
   {
    this.label1.Text += firstArray[i] + " ";
   }

FirstDouble(firstArray);

this.label1.Text += "\n\nContents of firstArray after calling FirstDouble.\n\t";

for(int i=0;i<firstArray.Length;i++)
   {
    this.label1.Text += firstArray[i] + " ";
   }

if(firstArray == firstArrayCopy)
    this.label1.Text +="\n\nThe references refer to the same array.\n";
   else
    this.label1.Text +="\n\nThe reference refer to different arrays.\n";

int[] secondArray = {1, 2, 3};
   int[] secondArrayCopy = secondArray;

   this.label1.Text += "\nTest passing secondArray reference by reference.";
   this.label1.Text += "\n\nContents of secondArray before calling SecondDouble:\n\t";

for(int i=0;i<secondArray.Length; i++)
   {
    this.label1.Text += secondArray[i] + " ";
   }

SecondDouble(ref secondArray);
   this.label1.Text +="\n\nContents of secondArray after calling SecondDouble:\n\t";

   for(int i=0; i<secondArray.Length;i++)
   {
    this.label1.Text += secondArray[i] + " ";
   }

if(secondArray== secondArrayCopy)
    this.label1.Text += "\n\nThe reference refer to the same array.";
   else
    this.label1.Text += "\n\nThe reference refer to different arrays.";

this.label1.Text += "\n___________________heshi_________________\nsecondarray\n";

for(int i = 0;i<secondArray.Length;i++)
   {
    this.label1.Text += secondArray[i] + " ";
   }
   this.label1.Text +="\nsecondarraycopy\n";
   for(int i=0;i<secondArrayCopy.Length;i++)
   {
    this.label1.Text += secondArrayCopy[i] + " ";
   }


  }

void FirstDouble(int[] array)
  {
   for(int i = 0;i<array.Length;i++)
    array[i] *= 2;
   array = new int[] {11, 12, 13};
  }

void SecondDouble(ref int[] array)
  {
   for(int i=0;i<array.Length;i++)
   {
    array[i] *= 2;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjfgww.html