正确理解C#中的ref关键字
来源:互联网 发布:易语言考勤系统源码 编辑:程序博客网 时间:2024/06/05 17:44
导读:
最近有人问到 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
usingSystem;
public classMyClass
{
public static voidTestRef(refchar i)
{
// The value of i will be changed in the calling method
i = 'b';
}
public static voidTestNoRef(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 voidMain()
{
char i = 'a'; // variable must be initialized
TestRef(refi); // the arg must be passed as ref
Console.WriteLine(i);
TestNoRef(i);
Console.WriteLine(i);
}
}
大家很容易看出输出结果是:
b
b
那么如果把这个例子做一些新的改动,将值类型(这里用的是 char)改成引用类型,程序运行又是什么效果呢?
// ----------------------------------------
// MyClass definition
public classMyClass
{
public intValue;
}
// ----------------------------------------
// Tester methods
public static voidTestRef(refMyClass m)
{
m.Value = 10;
}
public static voidTestNoRef(MyClass m)
{
m.Value = 20;
}
public static voidTestCreateRef(refMyClass m)
{
m = new MyClass();
m.Value = 100;
}
public static voidTestCreateNoRef(MyClass m)
{
m = new MyClass();
m.Value = 200;
}
public static voidMain()
{
MyClass m = newMyClass();
m.Value = 1;
TestRef(refm);
Console.WriteLine(m.Value);
TestNoRef(m);
Console.WriteLine(m.Value);
TestCreateRef(refm);
Console.WriteLine(m.Value);
TestCreateNoRef(m);
Console.WriteLine(m.Value);
}
大家能马上给出正确的答案么?如果能,那看来你对 ref 的用法了解得还是非常不错的。其实如果大家对 C++ 比较熟悉的话,把这段代码换成 C++ 的就好理解的多了。
// ----------------------------------------
// MyClass definition
#pragma once
classMyClass
{
public:
intValue;
};
typedefMyClass* MyClassPtr;
// ----------------------------------------
// Tester methods
voidTestRef(char* i)
{
*i = 'b';
}
voidTestNoRef(chari)
{
i = 'c';
}
voidTestRef(MyClassPtr* m)
{
(*m)->Value = 10;
}
voidTestNoRef(MyClassPtr m)
{
m->Value = 20;
}
voidTestCreateRef(MyClassPtr* m)
{
delete(*m);
*m = newMyClass();
(*m)->Value = 100;
}
voidTestCreateNoRef(MyClassPtr m)
{
m = newMyClass();
m->Value = 200;
}
intmain(intargc, char* argv[])
{
charc = 'a';
TestRef(&c);
printf("%cn", c); // output: b
TestNoRef(c);
printf("%cn", c); // output: b
MyClassPtr m = newMyClass;
m->Value = 1;
TestRef(&m);
printf("%dn", m->Value);
TestNoRef(m);
printf("%dn", m->Value);
TestCreateRef(&m);
printf("%dn", m->Value);
TestCreateNoRef(m);
printf("%dn", m->Value);
deletem;
return0;
}
这两段分别用 C# 和 C++ 实现的代码的输出结果都是一样的。后面用 MyClass 测试的输出结果是:
10
20
100
100
具体的原因相信经过大家的分析应该会很清楚的。另外如果大家有兴趣可以用 structs 再试试,也可以同时对 structs 在 C++ 和 C# 中的区别有进一步的认识。
...
最近有人问到 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
usingSystem;
public classMyClass
{
public static voidTestRef(refchar i)
{
// The value of i will be changed in the calling method
i = 'b';
}
public static voidTestNoRef(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 voidMain()
{
char i = 'a'; // variable must be initialized
TestRef(refi); // the arg must be passed as ref
Console.WriteLine(i);
TestNoRef(i);
Console.WriteLine(i);
}
}
大家很容易看出输出结果是:
b
b
那么如果把这个例子做一些新的改动,将值类型(这里用的是 char)改成引用类型,程序运行又是什么效果呢?
// ----------------------------------------
// MyClass definition
public classMyClass
{
public intValue;
}
// ----------------------------------------
// Tester methods
public static voidTestRef(refMyClass m)
{
m.Value = 10;
}
public static voidTestNoRef(MyClass m)
{
m.Value = 20;
}
public static voidTestCreateRef(refMyClass m)
{
m = new MyClass();
m.Value = 100;
}
public static voidTestCreateNoRef(MyClass m)
{
m = new MyClass();
m.Value = 200;
}
public static voidMain()
{
MyClass m = newMyClass();
m.Value = 1;
TestRef(refm);
Console.WriteLine(m.Value);
TestNoRef(m);
Console.WriteLine(m.Value);
TestCreateRef(refm);
Console.WriteLine(m.Value);
TestCreateNoRef(m);
Console.WriteLine(m.Value);
}
大家能马上给出正确的答案么?如果能,那看来你对 ref 的用法了解得还是非常不错的。其实如果大家对 C++ 比较熟悉的话,把这段代码换成 C++ 的就好理解的多了。
// ----------------------------------------
// MyClass definition
#pragma once
classMyClass
{
public:
intValue;
};
typedefMyClass* MyClassPtr;
// ----------------------------------------
// Tester methods
voidTestRef(char* i)
{
*i = 'b';
}
voidTestNoRef(chari)
{
i = 'c';
}
voidTestRef(MyClassPtr* m)
{
(*m)->Value = 10;
}
voidTestNoRef(MyClassPtr m)
{
m->Value = 20;
}
voidTestCreateRef(MyClassPtr* m)
{
delete(*m);
*m = newMyClass();
(*m)->Value = 100;
}
voidTestCreateNoRef(MyClassPtr m)
{
m = newMyClass();
m->Value = 200;
}
intmain(intargc, char* argv[])
{
charc = 'a';
TestRef(&c);
printf("%cn", c); // output: b
TestNoRef(c);
printf("%cn", c); // output: b
MyClassPtr m = newMyClass;
m->Value = 1;
TestRef(&m);
printf("%dn", m->Value);
TestNoRef(m);
printf("%dn", m->Value);
TestCreateRef(&m);
printf("%dn", m->Value);
TestCreateNoRef(m);
printf("%dn", m->Value);
deletem;
return0;
}
这两段分别用 C# 和 C++ 实现的代码的输出结果都是一样的。后面用 MyClass 测试的输出结果是:
10
20
100
100
具体的原因相信经过大家的分析应该会很清楚的。另外如果大家有兴趣可以用 structs 再试试,也可以同时对 structs 在 C++ 和 C# 中的区别有进一步的认识。
...
- 正确理解C#中的ref关键字
- 正确理解 C# 中的 ref 关键字
- 正确理解 C# 中的 ref 关键字
- 正确理解C#中的ref关键字
- 正确理解 C# 中的 ref 关键字
- 正确理解C#中的ref关键字
- 正确理解 C# 中的 ref 关键字 (续)
- C# 中的 ref 关键字
- 理解C#中的ref关键字
- C# 中的关键字 out ref
- C# 中的ref & out关键字 的区别
- C#中的ref out param关键字
- C#中的ref out param关键字
- C#中的两个关键字ref和out
- 关于C#中的ref和out关键字
- C#中的ref out param关键字
- C#中的ref和out关键字
- c# ref关键字
- HTTP 500 - 内部服务器错误
- 网站的增量设计:如何应对多变的用户需求
- Ext2 常见界面界面(grid分页、窗口布局、下拉框级联)
- 颠覆经典 拥抱变化---由自然灾害得到的教训
- C# 应用程序类中定义ACCESS数据库文件地址的方法
- 正确理解C#中的ref关键字
- asp.net MasterPage FindControl
- 运维服务管理的难点
- 动态sql语句基本语法
- test
- mfc扩展dll导出类的制作方法
- 关于session的详细解释
- ASP.NET Application,Session,Cookie和ViewState等对象用法和区别
- 弹出式窗体的实现方法