一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

ASP.NET Ref和Out關(guān)鍵字區(qū)別分析

  1. 值類型
  2. 引用類型

以C#為例:其值類型為sbyte,byte,char,short,ushort,int,uint,long和ulong,float和double,當(dāng)然還有decimal和bool。而引用類型則是string和object。

我想說(shuō)的

我想說(shuō)的就是――Ref和Out把我弄糊涂的原因是,當(dāng)時(shí)沒(méi)有認(rèn)真的去分析它對(duì)不同類型所做出的不同的動(dòng)作。

 

對(duì)于值類型。

使用了Ref和Out的效果就幾乎和C中使用了指針變量一樣。它能夠讓你直接對(duì)原數(shù)進(jìn)行操作,而不是對(duì)那個(gè)原數(shù)的Copy進(jìn)行操作。舉個(gè)小例子:

using System;namespace ConsoleApplication4
{
     /// <summary>
     /// Class1 的摘要說(shuō)明。
     /// </summary>
 class Class1
 {
      /// <summary>
      /// 應(yīng)用程序的主入口點(diǎn)。
      /// </summary>
  [STAThread]
  static void Main(string[] args)
  {
       int a = 5;
       int b;       squareRef(ref a);
       squareOut(out b);       Console.WriteLine("The a in the Main is: " + a);
       Console.WriteLine("The b in the Main is: " + b);
  }  static void squareRef(ref int x)
  {
       x = x * x;
       Console.WriteLine("The x in the squareRef is: " + x);
  }  static void squareOut(out int y)
  {
       y = 10;
       y = y * y;
       Console.WriteLine("The y in the squareOut is: " + y);
  }
 }
}

顯示的結(jié)果就是――25 100 25 100。

這樣的話,就達(dá)到了和C中的指針變量一樣的作用。

對(duì)于引用類型。

對(duì)于引用類型就比較難理解了。

先要了解到這一層――就是當(dāng)一個(gè)方法接收到一個(gè)引用類型的變量的時(shí)候,它將獲得這個(gè)引用(Reference)的一個(gè)Copy。由于Ref關(guān)鍵字可以用來(lái)向方法傳遞引用。所以,如果這個(gè)功能被誤用了――比如:當(dāng)一個(gè)如數(shù)組類型的引用對(duì)象用關(guān)鍵字Ref傳遞的時(shí)候,被調(diào)用的方法實(shí)際上已經(jīng)控制了傳遞過(guò)來(lái)的引用本身。這樣將使得被調(diào)用方法能用不同的對(duì)象甚至NULL來(lái)代替調(diào)用者的原始引用!

如圖。內(nèi)存地址為2000的變量arrayA中其實(shí)存放著數(shù)組{1,2,3,4,……}的內(nèi)存起始地址10000。如果一個(gè)方法fun()使用fun( arrayA[] )的話,它將順利的獲得數(shù)據(jù)10000,但這個(gè)10000將放在一個(gè)Copy中,不會(huì)放到內(nèi)存的2000位置。而這個(gè)時(shí)候我們?nèi)绻褂胒un( ref arrayA[] )的話,我們得到的值就是2000啦(也就是說(shuō),被調(diào)用方法能夠修改掉arrayA中的那個(gè)引用,使之不再指向10000,甚至可以用NULL來(lái)代替10000,這樣的話,那個(gè)10000地址中的數(shù)據(jù)可能就要被垃圾回收機(jī)制清理了。)

有個(gè)例子:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace RefOut
{
 /// <summary>
 /// Form1 的摘要說(shuō)明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Label label1;
  /// <summary>
  /// 必需的設(shè)計(jì)器變量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗體設(shè)計(jì)器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
   //
  }

  /// <summary>
  /// 清理所有正在使用的資源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗體設(shè)計(jì)器生成的代碼
  /// <summary>
  /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內(nèi)容。
  /// </summary>
  private void InitializeComponent()
  {
   this.button1 = new System.Windows.Forms.Button();
   this.label1 = new System.Windows.Forms.Label();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.Dock = System.Windows.Forms.DockStyle.Top;
   this.button1.Location = new System.Drawing.Point(0, 0);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(480, 32);

; 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>
  /// 應(yīng)用程序的主入口點(diǎn)。
  /// </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;

   }
   array = new int[] {11, 12, 13};
  }
 }
}
運(yùn)行后的結(jié)果是:運(yùn)行后的結(jié)果

這個(gè)就說(shuō)明了被調(diào)用的程序已經(jīng)改變了原有的Reference。

總結(jié)

總的說(shuō)來(lái),Ref和Out這兩個(gè)關(guān)鍵字都能夠提供相似的功效,其作用也很像C中的指針變量。稍有不同之處是:

  1. 使用Ref型參數(shù)時(shí),傳入的參數(shù)必須先被初始化。而Out則不需要,對(duì)Out而言,就必須在方法中對(duì)其完成初始化。
  2. 使用Ref和Out時(shí)都必須注意,在方法的參數(shù)和執(zhí)行方法時(shí),都要加Ref或Out關(guān)鍵字。以滿足匹配。
  3. Out更適合用在需要Return多個(gè)返回值的地方,而Ref則用在需要被調(diào)用的方法修改調(diào)用者的引用的時(shí)候。

AspNet技術(shù)ASP.NET Ref和Out關(guān)鍵字區(qū)別分析,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 五月婷香 | 天天更新天天久久久更新影院 | 狠狠狠很橹影院 | 国产成人综合视频 | 精品在线一区 | 欧美人与禽片免播放 | 欧美日韩亚洲国产千人斩 | 欧美黑人两根巨大挤入 | 中文字幕天天躁日日躁狠狠躁97 | 91精品成人免费国产 | 午夜乩伦 | 亚洲成人手机在线 | 五月婷婷色综合 | 午夜亚洲国产 | 亚洲激情五月 | 狠久久| 亚洲成人福利在线 | 天天爱天天爽 | 精品玖玖玖视频在线观看 | 两性色 | 99久久精品全部 | 最新国产精品自拍 | 久热网| 99精品国产成人一区二区 | 欧美人最猛性xxxxx | 欧美精品第一页 | www.色网| 四虎永久在线精品视频播放 | 中文字幕一区二区三区四区五区人 | 成人区精品一区二区不卡亚洲 | 中文字幕精品在线 | 天天躁天天弄天天爱 | 在线观看污污网站 | 麻豆一区二区 | 久久er国产精品免费观看1 | 国产欧美自拍 | 成年人视频在线观看免费 | 国产在线观看精品一区二区三区91 | 999re6热在线精品视频思思 | 韩国毛片在线观看 | 中文一区二区在线观看 |