传递参数(C# 编程指南)(http://msdn.microsoft.com/zh-cn/library/0f66670z(VS.80).aspx)
2010-01-14 11:24
736 查看
在 C# 中,既可以通过值也可以通过引用传递参数。
(1).通过引用传递参数允许函数成员(方法、属性、索引器、运算符和构造函数)更改参数的值,并保持该更改。若要通过引用传递参数,请使用 ref 或 out 关键字。
(2).通过值传递值类型,通过值将变量 n 传递给方法 SquareIt。方法内发生的任何更改对变量的原始值无任何影响。
示例:通过值传递值类型
输出:
The value before calling the method: 5
The value inside the method: 25
The value after calling the method: 5
示例:通过引用传递值类型
C#
复制代码
class PassingValByRef
{
static void SquareIt(ref int x)
// The parameter x is passed by reference.
// Changes to x will affect the original value of x.
{
x *= x;
System.Console.WriteLine("The value inside the method: {0}", x);
}
static void Main()
{
int n = 5;
System.Console.WriteLine("The value before calling the method: {0}", n);
SquareIt(ref n); // Passing the variable by reference.
System.Console.WriteLine("The value after calling the method: {0}", n);
}
}
输出:
The value before calling the method: 5
The value inside the method: 25
The value after calling the method: 25
(1).通过引用传递参数允许函数成员(方法、属性、索引器、运算符和构造函数)更改参数的值,并保持该更改。若要通过引用传递参数,请使用 ref 或 out 关键字。
(2).通过值传递值类型,通过值将变量 n 传递给方法 SquareIt。方法内发生的任何更改对变量的原始值无任何影响。
示例:通过值传递值类型
class PassingValByVal
{
static void SquareIt(int x)
// The parameter x is passed by value.
// Changes to x will not affect the original value of x.
{
x *= x;
System.Console.WriteLine("The value inside the method: {0}", x);
}
static void Main()
{
int n = 5;
System.Console.WriteLine("The value before calling the method: {0}", n);
SquareIt(n); // Passing the variable by value.
System.Console.WriteLine("The value after calling the method: {0}", n);
}
}
输出:
The value before calling the method: 5
The value inside the method: 25
The value after calling the method: 5
示例:通过引用传递值类型
C#
复制代码
class PassingValByRef
{
static void SquareIt(ref int x)
// The parameter x is passed by reference.
// Changes to x will affect the original value of x.
{
x *= x;
System.Console.WriteLine("The value inside the method: {0}", x);
}
static void Main()
{
int n = 5;
System.Console.WriteLine("The value before calling the method: {0}", n);
SquareIt(ref n); // Passing the variable by reference.
System.Console.WriteLine("The value after calling the method: {0}", n);
}
}
输出:
The value before calling the method: 5
The value inside the method: 25
The value after calling the method: 25
相关文章推荐
- MSDN(C# 编程指南)继承http://msdn.microsoft.com/zh-cn/library/ms173149(VS.80).aspx
- http://msdn.microsoft.com/zh-cn/library/aa942741(VS.80).aspx
- http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.gridview.rowediting.aspx
- http://msdn.microsoft.com/zh-cn/library/system.web.webpages(v=vs.111).aspx
- wpf msdn在线地址http://msdn.microsoft.com/zh-cn/library/ms752324(v=vs.110).aspx
- http://msdn.microsoft.com/zh-cn/default.aspx
- Cbitmap GetBitmapBits Copies the bits of the specified bitmap into the specified buffer http://msdn.microsoft.com/en-US/library/txffs5y2(v=vs.80).aspx
- VS 2003 的安装教程http://msdn.microsoft.com/zh-cn/vstudio/default.aspx
- http://msdn.microsoft.com/zh-cn/library/ms171728(v=VS.100).aspx
- C#中的问号 - http://msdn.microsoft.com/zh-cn/practices/dd391752.aspx
- http://msdn.microsoft.com/zh-cn/vbasic/ktzf2d23(VS.95).aspx
- http://msdn.microsoft.com/zh-cn/library/cc838145(VS.95).aspx
- http://msdn.microsoft.com/zh-cn/windowsphone/default.aspx?ocid=ban-n-cn-wp7--csdn
- http://www.cnblogs.com/jelea/archive/2009/02/07/1385852.html http://www.microsoft.com/china/msdn/events/webcasts/shared/Webcast/MSDNWebCast.aspx
- http://technet.microsoft.com/zh-cn/cc731957(WS.10).aspx
- wcf 跟踪和消息日志记录 :http://msdn.microsoft.com/zh-cn/library/ms751526.aspx
- http://msdn.microsoft.com/zh-cn/gg502456
- 获得Windows的版本及平台(http://msdn.microsoft.com/en-us/library/windows/desktop/ms724833(v=vs.85).aspx)
- NPP 示例:演示 Windows 消息处理 API (MAPI) http://msdn2.microsoft.com/zh-cn/library/ms177535(VS.80).aspx
- SQL Server 重新生成系统数据库(转:http://msdn.microsoft.com/zh-cn/library/dd207003.aspx)