• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >ASP.NET > ASP.NET Ref和Out关键字区别分析

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

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-11

通过本文主要向大家介绍了asp.net,asp net培训,asp和asp.net的区别,零基础学asp.net,c#和asp.net的区别等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
  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
{
     /// <summary>
     /// Class1 的摘要说明。
     /// </summary>
 class Class1
 {
      /// <summary>
      /// 应用程序的主入口点。
      /// </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);
  }
 }
}

显示的结果就是——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
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Label label1;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

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

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

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

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </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>
  /// 应用程序的主入口点。
  /// </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";
  &n

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Asp.net SignalR 应用并实现群聊功能 开源代码
  • asp.net动态更新
  • asp.net利用母版制作页脚效果
  • Asp.Net服务器发送HTTP标头后无法设置内容类型的问题解决
  • 使用asp.net mvc,boostrap及knockout.js开发微信自定义菜单编辑工具(推荐)
  • 详解ASP.NET MVC 常用扩展点:过滤器、模型绑定
  • ASP.NET Core发送邮件的方法
  • 在ASP.NET Core 中发送邮件的实现方法(必看篇)
  • ASP.NET MVC从视图传参到控制器的几种形式
  • Asp.net core WebApi 使用Swagger生成帮助页实例

相关文章

  • 2017-05-11ASP.NET过滤HTML字符串方法总结
  • 2017-05-11vs2010 vs2008 vs2005 快捷键大全
  • 2017-05-11SqlCommandBuilder类批量更新excel或者CSV数据的方法
  • 2017-05-11asp.net request.PathInfo实现的url重写
  • 2017-05-11ASP.NET2.0数据库入门之SQL Server
  • 2017-05-11Asp.net中防止用户多次登录的方法
  • 2017-05-11DataGrid 动态添加模板列 实现代码
  • 2017-05-11ASP.NET操作EXCEL的总结篇
  • 2017-05-11C#开发微信 二维码鼠标滑动 图像显示隐藏效果(推荐)
  • 2017-05-11asp.net gridview代码绑定

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • asp.net ajax实现无刷新验证码
    • ASP.NET MVC使用Ajax的辅助的解决方法
    • asp.net下通过泛解析和伪静态实现二级域名的实现方法
    • 浅谈.Net并行计算之数据并行
    • asp.NET中实现文件的压缩和解压(3种方式)
    • ASP.net全局程序文件Global.asax用法分析
    • asp.net Ajax 安装与卸载方法
    • 使用asp.net的pageDataSource中时索引问题解决方法
    • asp.net防止刷新时重复提交(可禁用工具条刷新按钮)
    • Asp.net给站点某目录增加Aspnet用户

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有