首页 技术 正文
技术 2022年11月16日
0 收藏 974 点赞 4,714 浏览 2104 个字

c#的参数传递有三种方式:
值传递,和c一样,
引用传递,类似与c++,但形式不一样
输出参数,这种方式可以返回多个值,这种有点像c中的指针传递,但其实不太一样。
值传递不细说,c中已经很详细了
引用传递实例如下:需要使用ref关键字

using System;
namespace CalculatorApplication
{
class NumberManipulator
{
//引用传递必须使用ref
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(); }
}
}

按输出传递参数
return 语句可用于只从函数中返回一个值。但是,可以使用 输出参数 来从函数中返回两个值。输出参数会把方法输出的数据赋给自己,其他方面与引用参数相似。
使用关键字out。
下面的实例演示了这点:

using System;namespace CalculatorApplication
{
class NumberManipulator
{
//使用out可以吧x的值重新复制给x,相当于更新
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(); }
}
}

  有out之后,a的值就变成了5。

两个参数的实例:

using System;namespace CalculatorApplication
{
class NumberManipulator
{
//使用out可以吧x的值重新复制给x,相当于更新
public void getValue(out int x, out int y)
{
int temp = 5;
x = temp; y = x;
} static void Main(string[] args)
{
NumberManipulator n = new NumberManipulator();
/* 局部变量定义 */
int a = 100;
int b = 20; Console.WriteLine("在方法调用之前,a 的值: {0}, b的值:{1}", a, b); /* 调用函数来获取值 */
n.getValue(out a, out b); Console.WriteLine("在方法调用之前,a 的值: {0}, b的值:{1}", a, b);
Console.ReadLine(); }
}
}

  再说一点就是,vs的ide功能,的确是很强大的,很多语法提示都有。

下面还有一个用处:

提供给输出参数的变量不需要赋值。当需要从一个参数没有指定初始值的方法中返回值时,输出参数特别有用。请看下面的实例,来理解这一点:
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();
}
}
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898