c#的参数调用
2024-08-29 23:23:56
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();
}
}
}
最新文章
- 3D滚动下拉菜单-简直不要太任性
- Android Studio debug使用release的签名
- eclipse JAVA实现AES的加密和解密算法
- ruby的在ubuntu上的安装
- COM编程之一 组件
- C#面向对象——方法的重载及构造函数、静态对象。
- 安装CAD出现Error 1904.Module的解决方法
- AIDE支持实时错误检查、代码重构、代码智能导航、生成APK
- MyBatis Generator代码自动生成工具的使用
- 【JAVA零基础入门系列】Day13 Java类的继承与多态
- Python可变参数*和**
- Thymleaf中th:each标签遍历list如何获取index
- [python]Python代码安全分析工具(Bandit)
- Log4J从基础到应用
- python中利用上下文管理器来实现mysql数据库的封装
- 通过gmapping和伪造的odom,完成Kinect建图
- 2-微信小程序开发(开发界面说明,按钮点击切换显示内容)
- centos安装jdk1.8
- oracle-rman-1
- Scala--操作符
热门文章
- Python学习之路:函数传递可变参数与不可变参数
- 同类控件的统一操作(以TCHECKBOX为例)
- 【LEETCODE】66、字符串分类,hard级别,题目:32,72,76
- CSS3弹性盒布局方式
- springboot自定义消息转换器HttpMessageConverter Spring Boot - 使用Gson替换Jackson
- 百度地图 libBaiduMapSDK_base_v4_2_1.so"; is 32-bit instead of 64-bit错误
- Java 之 Maven 基础
- old english diamaund钻石
- iOS学习——(转)解决iOS App打包后dSYM文件找不到的问题
- php在虚拟机和windows上的应用