C#一个方法返回多个值示例
时间:2023-06-22 18:38:49
static void Main(string[] args)
{
//声明
int value;
string strOutValue;
//调用函数
//函数的参数有两个返回的值
ReturnMulValue(out strOutValue,out value);
Console.WriteLine("Call ReturnMulValue(out string strOut, out int i)");
Console.WriteLine("value = {0:N}", value);//value = 1.00
Console.WriteLine("Out string value = " + strOutValue); //Out string value = out function
//调用函数 http://www.cnblogs.com/roucheng/
//函数原返回值与参数中均得到返回的值
value = ReturnMulValue(out strOutValue);
Console.WriteLine("Call ReturnMulValue(out string strOut)");
Console.WriteLine("value = {0:N}", value);//value = 2.00
Console.WriteLine("Out string value = " + strOutValue);//Out string value = out function 2
//声明
int[] outArr;
//调用函数
//函数返回数组
ReturnMulArr(out outArr, out value);
Console.WriteLine("Call ReturnMulArr(out int[] arr, out int i)");
Console.WriteLine("value = {0:N}", value);//value = 3.00
Console.WriteLine("Out arr value ");
for (int j = 0; j < outArr.Length; j++)
{
Console.Write(outArr[j]);//12345
}
}
/// <summary>
/// 返回多个值,原函数返回空
/// </summary>
/// <param name="strOut">使用out关键字返回的字符串</param>
/// <param name="i">使用out关键字返回的数值</param>
static void ReturnMulValue(out string strOut, out int i)
{
i = 1;
strOut = "out function";
}
/// <summary>
/// 返回多个值,原函数返回数值
/// </summary>
/// <param name="strOut">使用out关键字返回的字符串</param>
/// <returns>原函数直接返回的数值</returns>
static int ReturnMulValue(out string strOut)
{
int i = 2;
strOut = "out function 2";
return i;
}
/// <summary>
/// 返回数组
/// </summary>
/// <param name="arr">使用out关键字返回的数组</param>
/// <param name="i">使用out关键字返回的数值</param>
static void ReturnMulArr(out int[] arr, out int i)
{
arr = new int[5] { 1, 2, 3, 4, 5};
i = 3;
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MyBatis-Plus实现分页的方法使用详解
![](https://img.aspxhome.com/file/2023/6/60026_0s.png)
Android实现动态添加数据与堆叠折线图详解流程
![](https://img.aspxhome.com/file/2023/0/106000_0s.gif)
ViewDragHelper实现QQ侧滑效果
Java使用FFmpeg处理视频文件的方法教程
C#打印日志的方法总结
![](https://img.aspxhome.com/file/2023/4/96334_0s.jpg)
Java for-each循环使用难题2例(高级使用方法)
BroadcastReceiver静态注册案例详解
![](https://img.aspxhome.com/file/2023/6/139286_0s.gif)
Java用freemarker导出word实用示例
![](https://img.aspxhome.com/file/2023/5/86715_0s.png)
详解Java Synchronized的实现原理
![](https://img.aspxhome.com/file/2023/7/60037_0s.png)
Android ListView position详解及实例代码
在 C# 中使用 Span<T> 和 Memory<T> 编写高性能代码的详细步骤
![](https://img.aspxhome.com/file/2023/8/96388_0s.png)
C#中按引用传递与按值传递的区别,以及ref与out关键字的用法详解
Spring定时任务使用及如何使用邮件监控服务器
MyBatis if test 判断字符串相等不生效问题
C#多线程中的异常处理操作示例
![](https://img.aspxhome.com/file/2023/4/113824_0s.png)
C#中decimal保留2位有效小数的实现方法
Android中CountDownTimer 实现倒计时功能
![](https://img.aspxhome.com/file/2023/4/120144_0s.gif)
C语言实现矩阵运算案例详解
![](https://img.aspxhome.com/file/2023/4/94604_0s.png)
Android获取热点主机ip和连接热点手机ip的代码
springboot-2.3.x最新版源码阅读环境搭建(基于gradle构建)
![](https://img.aspxhome.com/file/2023/9/121929_0s.jpg)