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();
}
}
}

  

c#的参数调用的更多相关文章

  1. Atitit main函数的ast分析  数组参数调用的ast astview解析

    Atitit main函数的ast分析  数组参数调用的ast astview解析 1.1. Xxcls.main(new String[]{"","bb"}) ...

  2. PHP通过传递对象参数调用asp.net Webservice 服务

    asp.net 测试服务 ProcessRequest.asmx文件代码  public class ProcessRequest : System.Web.Services.WebService   ...

  3. JavaScript函数定义 ,参数调用

    一.JavaScript函数函数: 函数就是一种封装,由事件驱动的或者当它被调用时执行的可重复使用的代码块.定义函数:function 函数名(){函数体;}数不会自动执行,需要被调用才可以执行函数名 ...

  4. Scala 按名称参数调用函数 与 =>的用法

    转自:http://blog.csdn.net/shenxiaoming77/article/details/54835679 通常情况下,函数的参数是传值参数:即参数的值在它被传递给函数之前被确定. ...

  5. 可以使用可用的服务和参数调用在“eWorld.WCFImplement.ServiceImplement.ImageArchiveService”类型上使用“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”找到的构造函数: 无法解析参数'eWorld.WCFBLL.ImageArchive.IDocumentOperation

    可以使用可用的服务和参数调用在“eWorld.WCFImplement.ServiceImplement.ImageArchiveService”类型上使用“Autofac.Core.Activato ...

  6. python定义函数时的参数&调用函数时的传参

    一.定义函数: 1.位置参数:直接定义参数 2.默认参数(或者关键字参数):参数名 = "默认值" 3.位置参数必须在默认参数之前 二.调用函数: 1.按位置传,直接写参数的值 2 ...

  7. 使用“1”个参数调用“DownloadString”时发生异常:“操作超时”

    我今天在终端美化时间遇到一个问题是这样的 使用“1”个参数调用“DownloadString”时发生异常:“操作超时” 然后网我看了下,访问链接属于https的东西,根据直觉我觉得是这样的,是由于访问 ...

  8. 使用“2”个参数调用“SetData”时发生异常:“程序集“

    使用"2"个参数调用"SetData"时发生异常:"程序集"Microsoft.VisualStudio.ProjectSystem.VS. ...

  9. 不封装ajax 带url参数调用接口

    html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  10. [Effective JavaScript 笔记]第21条:使用apply方法通过不同数量的参数调用函数

    apply()方法定义 函数的apply()方法和call方法作用相同,区别在于接收的参数的方式不同.apply()方法接收两个参数,一个是对象,一个是参数数组. apply()作用 1.用于延长函数 ...

随机推荐

  1. js中常见的创建对象的方法(1)

    工厂模式:抽象了创建具体对象的过程 function createPerson(name, age, job){ var obj = new Object(); obj.name = name; ob ...

  2. python学习-32 zip函数

    zip 拉链方法 例如:1. ')))) 运行结果: [(')] Process finished with exit code 0 2. a = {'name':'abc','age':18,'ad ...

  3. Rust 智能指针(二)

    1. Rc<T> 引用计数指针 Rc<T> 是引用计数指针,可以使用clone使得指针所指向的数据具有多个所有者. enum List { Cons(i32, Rc<Li ...

  4. [动图演示]Redis 持久化 RDB/AOF 详解与实践【华为云技术分享】

    Redis 是一个开源( BSD 许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件.它支持的数据类型很丰富,如字符串.链表.集 合.以及散列等,并且还支持多种排序功能. 什么叫持 ...

  5. 24H玩转 Grafana 被工程师称相当专业,如何做到?

    国庆假期发生了两件小事,其一是我默默度过 35 周岁生日,其二是玩了下grafana `并在节后第一天被工程师 M 称赞:相当专业. 1.我为什么要玩 grafana 呢? 数月前我提交了一份数据后台 ...

  6. sping boot/cloud配置文件 on 读取为true

    sping boot/cloud配置文件 on 读取为true 原文地址:https://blog.csdn.net/hb9176070/article/details/82749771 最近在写sp ...

  7. Eclipse的常用设置。

    用惯了VS,再回过去用Eclipse真是一件痛苦的事.so,在这里记录下使用过程中的一些设置以做备忘. 1.代码自动提示 在我们忘记方法名或者想偷懒时,代码自动提示很管用.不过Eclipse默认是输入 ...

  8. docker安装mysql笔记

    首先 查找镜像 docker search mysql 拉取镜像 : docker pull mysql 拉取成功后,查看本地镜像: docker images 可以看到本地有两个镜像(redis是我 ...

  9. 神奇的外部嵌套(使用ROW_NUMBER()查询带条件的时候提示列名无效)

    declare @pageIndex int -- 第几页 declare @pageSize int -- 每页包含的记录数 --这里注意一下,不能直接把变量放在这里,要用select select ...

  10. 解决SqlDataSource连接超时的问题

    采用两种策略: 1.连接字符串增加Connect Timeout=1000(大约1000秒/60=16分钟) 2.设置SqlDataSourced 的 EnableCaching="True ...