C#难点逐个击破(2):out返回参数
时间:2022-02-02 01:24:18
传值(by value)与传址(by reference)分别为普通传递参数方式与ref声明方式,传址方式在使用前需要ref关键词修饰;out为输出返回传递,与ref相同之处较多,其重要区别是在上一节讲到的ref在被引用方法中必须被初始化;out在被引用方法中一定要有返回值。
using System;
/******************************
* Chapter:C#难点逐个击破(一)
* Author:王洪剑
* Date:2010-1-15
* Blog:http://www.51obj.cn/
* Email:walkingp@126.com
* Description:重点讲解输出参数out
* ***************************/
namespace TestOut
{
class Program
{
/// <summary>
/// out的输出值、返回值
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
static string OutResultMethod(out string name)
{
string _name = "Wang Hongjian";
name = _name;
string __name = "Zheng Zi";
return __name;
}
static void Main(string[] args)
{
string _name = "Zhang Yu";
Console.WriteLine("调用前:{0}", _name);
string result= OutResultMethod(out _name);
Console.WriteLine("调用后:{0}",_name);
Console.WriteLine("返回结果:{0}",result);
Console.ReadKey();
}
}
}
运行结果:
out在实际项目中使用较多,这是因为out可以返回多个值
class Program
{
/// <summary>
/// out的输出值、返回值
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
static string OutResultMethod(out string name,out string password)
{
string _name = "Wang Hongjian";
name = _name;
string _password = "123456";
password = _password;
string __name = "Zheng Zi";
return __name;
}
static void Main(string[] args)
{
string _name = "Zhang Yu";
string _password;
Console.WriteLine("调用前:{0}", _name);
string result = OutResultMethod(out _name, out _password);//多个参数传递
Console.WriteLine("调用后:{0}",_name);
Console.WriteLine("返回结果:{0}",result);
Console.ReadKey();
}
}
联想多个参数,你一定想到了使用数组吧,不错,使用数组是个很好的方式,数组本身把同一类型的参数打包“封装"为一个对象传递给方法,这样可以简化多参数方法的编写,还能实现类似重载的效果。
static void OutArrayMethod(out string[] strArr)
{
int i = 0;
string[] _strArr = new string[10];
while (i < _strArr.Length)
{
_strArr[i] = "第" + i + "个成员";
i++;
}
strArr = _strArr;
}
static void Main(string[] args)
{
string[] _strArr=new string[10];
OutArrayMethod(out _strArr);
foreach (string str in _strArr)
{
Console.WriteLine(str);
}
Console.ReadKey();
}
运行效果:
源码下载
标签:C#,out,返回参数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Boot 整合JPA 数据模型关联使用操作(一对一、一对多、多对多)
2022-11-18 23:14:29
Java由浅入深带你精通继承super
2023-11-23 02:34:28
![](https://img.aspxhome.com/file/2023/7/59737_0s.png)
C#基础知识之new关键字介绍
2021-09-24 02:48:26
![](https://img.aspxhome.com/file/2023/3/72493_0s.png)
Java单例模式的线程安全,饿汉和懒汉模式详解
2022-05-31 16:12:08
C# NetRemoting实现双向通信
2022-07-01 14:05:58
服务器端C#实现的CSS解析器
2022-01-25 12:26:20
Spring实战之Bean的作用域singleton和prototype用法分析
2023-03-23 20:48:37
java开发_图片截取工具实现原理
2023-10-23 22:52:06
![](https://img.aspxhome.com/file/2023/0/74880_0s.png)
SpringBoot 整合mapstruct的实现步骤
2023-11-28 00:51:00
![](https://img.aspxhome.com/file/2023/0/60320_0s.png)
通过实例学习Either 树和模式匹配
2023-05-21 02:02:41
![](https://img.aspxhome.com/file/2023/1/62131_0s.gif)
android中RecyclerView自定义分割线实现
2023-08-03 17:37:47
![](https://img.aspxhome.com/file/2023/0/114360_0s.png)
SpringBoot使用Atomikos技术整合多数据源的实现
2021-11-08 23:46:23
Java实现SSL双向认证的方法
2023-09-22 10:34:35
![](https://img.aspxhome.com/file/2023/0/58090_0s.jpg)
Springboot处理配置CORS跨域请求时碰到的坑
2023-12-20 11:40:45
![](https://img.aspxhome.com/file/2023/3/95983_0s.png)
详解MyBatis直接执行SQL查询及数据批量插入
2021-12-02 17:52:08
java单例模式学习示例
2022-03-29 19:03:41
详解Spring中bean的几种注入方式
2023-02-12 20:25:07
IDEA MyBatis Plugins自动生成实体类和mapper.xml
2021-07-24 01:26:37
![](https://img.aspxhome.com/file/2023/8/83048_0s.jpg)
Maven学习----Maven安装与环境变量配置教程
2021-12-04 08:20:25
![](https://img.aspxhome.com/file/2023/8/60678_0s.png)
详解springmvc常用5种注解
2023-03-27 15:15:43
![](https://img.aspxhome.com/file/2023/4/68514_0s.png)