All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
Sql表数据操作_mssql学习_编程技术
PHP中的session的几个问题_php资料_编程技术
iframe的高度随内容的变化而变化_[Html教程]
C#中的foreach语句使用方法
PHP程序安全基础:手工php注入总结_php资料_编程技术
ASP.NET2.0,GridView绑定XmlDocument_[Asp.Net教程]
在Visual,C#程序中使用系统热键_.net资料_编程技术
实例:在ASP.NET中创建使用全球唯一标识符_.net资料_编程技术
ASP.NET日历控件二次开发和使用_[Asp.Net教程]
ASP.NET2.0实现网站的自动升级_[Asp.Net教程]
Reading number is top 10 pictures
A man's favorite things10
The real super beauty8
Group of female porn in 《westwards》, uninhibited woman threatened to not the bottom line2
The Soviet union swimsuit exposure in the 70 year1
奇趣的世界记录3
Original author said, this is the Hengyang people
The hot big eye big breast beauty1
PengLiYuan lady for the world health organization fight TB and AIDS goodwill ambassador
From China fortress sora aoi4
Average female college students3
Download software ranking
Red cliff
虚拟机汉化软件
1400篇各类破解文章
WebService在.NET中的实战应用教学视频 → 第4集
在线棋牌游戏3.05版
Dance with duck(male prostitution)
Tram sex maniac 2 (H) rar bag11
Boxer's Top ten classic battle5
Tram sex maniac 2 (H) rar bag19
Unix video tutorial6
aaa published in(发表于) 2013/12/18 8:20:48 Edit(编辑)
正确理解C#中的ref关键字_.net资料_编程技术

正确理解C#中的ref关键字_.net资料_编程技术

正确理解C#中的ref关键字_.net资料_编程技术-你的首页-uuhomepage.com

  最近有人问到 ref 关键字的正确用法,下面我们来举例说明。其实要更好的理解 ref 关键字,结合 C++ 代码更加容易一些。另外在开始我们的例子之前,需要提前说明几点:


  • C# 中的数据有两种类型:引用类型(reference types)和值类型(value types)。 简单类型(包括int, long, double等)和结构(structs)都是值类型,而其他的类都是引用类型。 简单类型在传值的时候会做复制操作,而引用类型只是传递引用,就像 C++ 中的指针一样。
  • 注意 structs 在 C# 和 C++ 中的区别。在 C++ 中, structs 和类基本相同(except that the default inheritance and default access are public rather than private)。 而在 C# 中,structs 和类有很大的区别。其中最大的区别(我个人觉得,同时也是容易忽略的一个地方)可能就是它是值类型,而不是引用类型。


  下面这段代码是 MSDN 中的例子:

// cs_ref.cs
using System;
public class MyClass
{
public static void TestRef(ref char i)
{
// The value of i will be changed in the calling method
i = 'b';
}

public static void TestNoRef(char i)
{
// The value of i will be unchanged in the calling method
i = 'c';
}

// This method passes a variable as a ref parameter; the value of the
// variable is changed after control passes back to this method.
// The same variable is passed as a value parameter; the value of the
// variable is unchanged after control is passed back to this method.

public static void Main()
{
char i = 'a'; // variable must be initialized
TestRef(ref i); // the arg must be passed as ref
Console.WriteLine(i);
TestNoRef(i);
Console.WriteLine(i);
}
}

  大家很容易看出输出结果是:

b
b

  那么如果把这个例子做一些新的改动,将值类型(这里用的是 char)改成引用类型,程序运行又是什么效果呢?

// ----------------------------------------
// MyClass definition

public class MyClass
{
public int Value;
}


// ----------------------------------------
// Tester methods

public static void TestRef(ref MyClass m)
{
m.Value = 10;
}

public static void TestNoRef(MyClass m)
{
m.Value = 20;
}

public static void TestCreateRef(ref MyClass m)
{
m = new MyClass();
m.Value = 100;
}

public static void TestCreateNoRef(MyClass m)
{
m = new MyClass();
m.Value = 200;
}

public static void Main()
{
MyClass m = new MyClass();
m.Value = 1;

TestRef(ref m);
Console.WriteLine(m.Value);

TestNoRef(m);
Console.WriteLine(m.Value);

TestCreateRef(ref m);
Console.WriteLine(m.Value);

TestCreateNoRef(m);
Console.WriteLine(m.Value);
}

  大家能马上给出正确的答案么?如果能,那看来你对 ref 的用法了解得还是非常不错的。其实如果大家对 C++ 比较熟悉的话,把这段代码换成 C++ 的就好理解的多了。

// ----------------------------------------
// MyClass definition

#pragma once

class MyClass
{
public:
int Value;
};

typedef MyClass* MyClassPtr;


// ----------------------------------------
// Tester methods

void TestRef(char* i)
{
*i = 'b';
}

void TestNoRef(char i)
{
i = 'c';
}

void TestRef(MyClassPtr* m)
{
(*m)->Value = 10;
}

void TestNoRef(MyClassPtr m)
{
m->Value = 20;
}

void TestCreateRef(MyClassPtr* m)
{
delete (*m);
*m = new MyClass();
(*m)->Value = 100;
}

void TestCreateNoRef(MyClassPtr m)
{
m = new MyClass();
m->Value = 200;
}

int main(int argc, char* argv[])
{
char c = 'a';

TestRef(&c);
printf("%c\n", c); // output: b
TestNoRef(c);
printf("%c\n", c); // output: b

MyClassPtr m = new MyClass;
m->Value = 1;

TestRef(&m);
printf("%d\n", m->Value);

TestNoRef(m);
printf("%d\n", m->Value);

TestCreateRef(&m);
printf("%d\n", m->Value);

TestCreateNoRef(m);
printf("%d\n", m->Value);

delete m;

return 0;
}

  这两段分别用 C# 和 C++ 实现的代码的输出结果都是一样的。后面用 MyClass 测试的输出结果是:

10
20
100
100

  具体的原因相信经过大家的分析应该会很清楚的。另外如果大家有兴趣可以用 structs 再试试,也可以同时对 structs 在 C++ 和 C# 中的区别有进一步的认识。





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.