网站建设
  简约型网页设计套餐998
  实惠型网站建设套餐2580
  综合型网站制作套餐4980
  网站改版与网站维护
  行业网站建设方案
  大型网站建设解决方案
  企业网站建设流程
  帝网科技网站设计与网站制作
建站FAQ
·网站空间问题解答
·企业邮箱问题解答
 
酷站欣赏
·房产酷站(379)
·综合门户(8 9)
·建筑装饰(603)
·手机通讯(354)
·生活购物(376)
·医疗保健(199)
·文化摄影(602)
·休闲体育(399)
>>更多酷站欣赏
网站优化
·Google(谷歌)优化   ·百度(BaiDu)优化
·雅虎(Yahoo)优化    ·Alexa排名优化   
·Google AdSense   ·DMOZ目录提交  
建站知识
·网站建设知识·网站名词解释·网站运营知识
·网络营销知识·搜索引擎知识·实用技术文摘
网站推广
百度网站推广 google网站推广
搜狐网站推广 网易网站推广
新浪网站推广   雅虎网站推广
  您当前位置: 当前位置:帝网科技 >> web开发 >> .NET专栏 >> 浏览文章
 
 
ASP.NET教程:Ref和Out关键字异同
作者:编辑整理 来源:帝网科技 日期:2009年02月03日 点击数:


类型介绍

在几乎所有的OOP语言中,都存在2种类型的值。

  1. 值类型
  2. 引用类型

以C#为例:其值类型为sbyte,byte,char,short,ushort,int,uint,long和ulong,float和double,当然还有decimal和bool。而引用类型则是string和object。

我想说的

我想说的就是——Ref和Out把我弄糊涂的原因是,当时没有认真的去分析它对不同类型所做出的不同的动作。

 

对于值类型。

使用了Ref和Out的效果就几乎和C中使用了指针变量一样。它能够让你直接对原数进行操作,而不是对那个原数的Copy进行操作。举个小例子:

using System;namespace ConsoleApplication4
{
     ///


     /// Class1 的摘要说明。
     ///

 class Class1
 {
      ///
      /// 应用程序的主入口点。
      ///

  [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);
  }
 }
}

显示的结果就是——25 100 25 100。

这样的话,就达到了和C中的指针变量一样的作用。

对于引用类型。

对于引用类型就比较难理解了。

先要了解到这一层——就是当一个方法接收到一个引用类型的变量的时候,它将获得这个引用(Reference)的一个Copy。由于Ref关键字可以用来向方法传递引用。所以,如果这个功能被误用了——比如:当一个如数组类型的引用对象用关键字Ref传递的时候,被调用的方法实际上已经控制了传递过来的引用本身。这样将使得被调用方法能用不同的对象甚至NULL来代替调用者的原始引用!

点此在新窗口浏览图片

如图。内存地址为2000的变量arrayA中其实存放着数组{1,2,3,4,……}的内存起始地址10000。如果一个方法fun()使用fun( arrayA[] )的话,它将顺利的获得数据10000,但这个10000将放在一个Copy中,不会放到内存的2000位置。而这个时候我们如果使用fun( ref arrayA[] )的话,我们得到的值就是2000啦(也就是说,被调用方法能够修改掉arrayA中的那个引用,使之不再指向10000,甚至可以用NULL来代替10000,这样的话,那个10000地址中的数据可能就要被垃圾回收机制清理了。)

有个例子:

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

namespace RefOut
{
 ///


 /// Form1 的摘要说明。
 ///

 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Label label1;
  ///
  /// 必需的设计器变量。
  ///

  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  ///


  /// 清理所有正在使用的资源。
  ///

  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  ///


  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  ///

  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

  ///


  /// 应用程序的主入口点。
  ///

  [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   {
    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   {
    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   {
    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   {
    this.label1.Text += secondArray[i] + " ";
   }
   this.label1.Text +="\nsecondarraycopy\n";
   for(int i=0;i   {
    this.label1.Text += secondArrayCopy[i] + " ";
   }


  }

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

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

   }
   array = new int[] {11, 12, 13};
  }
 }
}
运行后的结果是:点此在新窗口浏览图片

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

点此在新窗口浏览图片

总结

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

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

  相关文章
 
·ASP.NET使用log4Net日志组件教程(日志
·ASP.NET MVC 框架
·C#实现的BinaryTree
·WebForms使用System.Web.Routing
·ASP.NET获取远程网页下载到本地文件
·一个“简单”的ASP.NET的服务器控件
·ASP.net与PHP两大网站开发架构优势对比
·教你七招提高.NET网站性能
·ASP.NET未来:简化开发 HTML5性能提升
·ASP.NET实现类似Excel的数据透视表
·FileUpload上传多文件出现错误的解决方
·.NET从优酷专辑中采集所有视频及信息(
·ASP.NET 4中的SEO改进
·详解Asp.net MVC DropDownLists
·提高ASP.NET应用程序性能的几招方法
·asp.net实现51job地区选择效果
·ASP.NET中创建GeoRSS订阅源
·ASP.NET 4.0开发更加简便
·ASP.NET页面间数据传递的方法
·ASP.NET的SEO:使用.ashx文件——排除
 
 

公司环境 | 合作伙伴 | 人才招聘 | 付款方式 | 关于我们

地址:广州市天河区中山大道中120号D805 电话:020-82529556 传真:020-82529556
广州帝网网络科技有限公司 版权所有 粤ICP备08119341号