C#梳理【参数传递】
来源:互联网 发布:部落冲突最新兵种数据 编辑:程序博客网 时间:2024/05/19 02:29
参数传递
当调用带有参数的方法时,您需要向方法传递参数。在 C# 中,有三种向方法传递参数的方式:
按值传递参数
这是参数传递的默认方式。在这种方式下,当调用一个方法时,会为每个值参数创建一个新的存储位置。
实际参数的值会复制给形参,实参和形参使用的是两个不同内存中的值。所以,当形参的值发生改变时,不会影响实参的值,从而保证了实参数据的安全。下面的实例演示了这个概念:
using System;namespace CalculatorApplication{ class NumberManipulator { public void swap(int x, int y) { int temp; temp = x; /* 保存 x 的值 */ x = y; /* 把 y 赋值给 x */ y = temp; /* 把 temp 赋值给 y */ } static void Main(string[] args) { NumberManipulator n = new NumberManipulator(); /* 局部变量定义 */ int a = 100; int b = 200; Console.WriteLine("在交换之前,a 的值: {0}", a); Console.WriteLine("在交换之前,b 的值: {0}", b); /* 调用函数来交换值 */ n.swap(a, b); Console.WriteLine("在交换之后,a 的值: {0}", a); Console.WriteLine("在交换之后,b 的值: {0}", b); Console.ReadLine(); } }}
当上面的代码被编译和执行时,它会产生下列结果:
在交换之前,a 的值:100在交换之前,b 的值:200在交换之后,a 的值:100在交换之后,b 的值:200
结果表明,即使在函数内改变了值,值也没有发生任何的变化。
按引用传递参数(ref)
引用参数是一个对变量的内存位置的引用。当按引用传递参数时,与值参数不同的是,它不会为这些参数创建一个新的存储位置。引用参数表示与提供给方法的实际参数具有相同的内存位置。
在 C# 中,使用 ref 关键字声明引用参数。下面的实例演示了这点:
using System;namespace CalculatorApplication{ class NumberManipulator { public void swap(ref int x, ref int y) { int temp; temp = x; /* 保存 x 的值 */ x = y; /* 把 y 赋值给 x */ y = temp; /* 把 temp 赋值给 y */ } static void Main(string[] args) { NumberManipulator n = new NumberManipulator(); /* 局部变量定义 */ int a = 100; int b = 200; Console.WriteLine("在交换之前,a 的值: {0}", a); Console.WriteLine("在交换之前,b 的值: {0}", b); /* 调用函数来交换值 */ n.swap(ref a, ref b); Console.WriteLine("在交换之后,a 的值: {0}", a); Console.WriteLine("在交换之后,b 的值: {0}", b); Console.ReadLine(); } }}
当上面的代码被编译和执行时,它会产生下列结果:
在交换之前,a 的值:100在交换之前,b 的值:200在交换之后,a 的值:200在交换之后,b 的值:100
结果表明,swap 函数内的值改变了,且这个改变可以在 Main 函数中反映出来。
按输出传递参数(out)
return 语句可用于只从函数中返回一个值。但是,可以使用 输出参数 来从函数中返回两个值。输出参数会把方法输出的数据赋给自己,其他方面与引用参数相似。
下面的实例演示了这点:
using System;namespace CalculatorApplication{ class NumberManipulator { public void getValue(out int x ) { int temp = 5; x = temp; } static void Main(string[] args) { NumberManipulator n = new NumberManipulator(); /* 局部变量定义 */ int a = 100; Console.WriteLine("在方法调用之前,a 的值: {0}", a); /* 调用函数来获取值 */ n.getValue(out a); Console.WriteLine("在方法调用之后,a 的值: {0}", a); Console.ReadLine(); } }}
当上面的代码被编译和执行时,它会产生下列结果:
在方法调用之前,a 的值: 100在方法调用之后,a 的值: 5
提供给输出参数的变量不需要赋值。当需要从一个参数没有指定初始值的方法中返回值时,输出参数特别有用。请看下面的实例,来理解这一点:
using System;namespace CalculatorApplication{ class NumberManipulator { public void getValues(out int x, out int y ) { Console.WriteLine("请输入第一个值: "); x = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入第二个值: "); y = Convert.ToInt32(Console.ReadLine()); } static void Main(string[] args) { NumberManipulator n = new NumberManipulator(); /* 局部变量定义 */ int a , b; /* 调用函数来获取值 */ n.getValues(out a, out b); Console.WriteLine("在方法调用之后,a 的值: {0}", a); Console.WriteLine("在方法调用之后,b 的值: {0}", b); Console.ReadLine(); } }}
当上面的代码被编译和执行时,它会产生下列结果(取决于用户输入):
请输入第一个值:7请输入第二个值:8在方法调用之后,a 的值: 7在方法调用之后,b 的值: 8
阅读全文
0 0
- C#梳理【参数传递】
- C#中的参数传递
- C#中的参数传递
- C#中的参数传递
- C#的参数传递
- C#多线程参数传递
- C#多线程参数传递
- C#多线程参数传递
- c#参数传递
- c# 参数传递类型
- C# 参数传递
- C# 多线程 参数传递
- C#中的参数传递
- C#中的参数传递
- C#参数传递方式
- c#参数传递
- C# 参数传递
- C#参数传递详解
- Shader学习之Cg语言一(Cg语言概述)
- bootstrap-datepicker限定可选时间范围 一、应用场景 实际应用中可能会根据日期字段查询某个日期范围内的数据,则需要对日期选择器可选时间进行限制, 如:开始时间不能大于结束时间,
- 通过Fiddler肆意修改接口返回数据进行测试
- C#综合揭秘——细说多线程(下)
- 四旋翼飞行器教学笔记2.2——姿态读取的计算
- C#梳理【参数传递】
- java 异常及异常使用总结
- Terms of Service
- 彻底征服 Spring AOP 之 实战篇
- CCF认证201509-2 日期计算
- eclipse,maven启动报错(非命令启动):JRebel: ERROR Class 'org.apache.catalina.loader.WebappClassLoader' could no
- 一舞倾城,再舞倾国,如之奈何
- greenplum 参数参数的具体方法
- shell中for循环从0迭代到999