加入收藏 | 设为首页 | 会员中心 | 我要投稿 焦作站长网 (https://www.0391zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > Asp教程 > 正文

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

发布时间:2020-03-14 12:05:57 所属栏目:Asp教程 来源:站长网
导读:类型介绍在几乎所有的OOP语言中,都存在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 += "nnContents of firstArray before calling FirstDouble:nt";

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

FirstDouble(firstArray);

this.label1.Text += "nnContents of firstArray after calling FirstDouble.nt";

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

if(firstArray == firstArrayCopy)
    this.label1.Text +="nnThe references refer to the same array.n";
   else
    this.label1.Text +="nnThe 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 += "nnContents of secondArray before calling SecondDouble:nt";

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

SecondDouble(ref secondArray);
   this.label1.Text +="nnContents of secondArray after calling SecondDouble:nt";

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

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

this.label1.Text += "n___________________heshi_________________nsecondarrayn";

for(int i = 0;i<secondArray.Length;i++)
   {
    this.label1.Text += secondArray[i] + " ";
   }
   this.label1.Text +="nsecondarraycopyn";
   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;

}
   array = new int[] {11, 12, 13};
  }
 }
}
运行后的结果是:

运行后的结果

这个就说明了被调用的程序已经改变了原有的Reference。

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

总结

总的说来,Ref和Out这两个关键字都能够提供相似的功效,其作用也很像C中的指针变量。稍有不同之处是:

使用Ref型参数时,传入的参数必须先被初始化。而Out则不需要,对Out而言,就必须在方法中对其完成初始化。 使用Ref和Out时都必须注意,在方法的参数和执行方法时,都要加Ref或Out关键字。以满足匹配。 Out更适合用在需要Return多个返回值的地方,而Ref则用在需要被调用的方法修改调用者的引用的时候。

(编辑:焦作站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读