【05】C#特有的ref、out参数
java和C#非常相似,它们大部分的语法是一样的,但尽管如此,也有一些地方是不同的。
为了更好地学习java或C#,有必要分清它们两者到底在哪里不同。
我们这次要来探讨C#特有的ref、out参数。
java代码:
public class HelloWorld {
public static int n1=10;
public static int n2=20;
public static void main(String[] args) {
System.out.println("n1:"+n1); //
System.out.println("n2:"+n2); //
Test();
System.out.println("----交换后----");
System.out.println("n1:"+n1); //
System.out.println("n2:"+n2); //
}
private static void Test() {
int temp=n1;
n1=n2;
n2=temp;
}
}
C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace HelloWorld
{
class Program
{
public static int n1 = ;
public static int n2 = ;
static void Main(string[] args)
{
Console.WriteLine("n1:" + n1); //
Console.WriteLine("n2:" + n2); //
Console.WriteLine("----交换后----");
Test();
Console.WriteLine("n1:" + n1); //
Console.WriteLine("n2:" + n2); //
Console.ReadKey();
} private static void Test()
{
int temp = n1;
n1 = n2;
n2 = temp;
}
}
}
C#还可以使用ref函数来实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace HelloWorld
{
class Program
{ static void Main(string[] args)
{
int n1 = ;
int n2 = ;
Console.WriteLine("n1:" + n1); //
Console.WriteLine("n2:" + n2); //
Console.WriteLine("----交换后----");
Test(ref n1, ref n2);
Console.WriteLine("n1:" + n1); //
Console.WriteLine("n2:" + n2); //
Console.ReadKey();
} private static void Test(ref int n1, ref int n2)
{
int temp = n1;
n1 = n2;
n2 = temp;
}
}
}
案例获得最大最小值:
java代码:
public class HelloWorld {
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4};
int[] res = GetMaxMin(nums);
System.out.println(String.format("最大值是:%d 最小值为:%d", res[0], res[1]));
}
private static int[] GetMaxMin(int[] nums) {
int[] res = new int[4];
//设res[0]为最大值,res[1]为最小值
res[0] = nums[0];
res[1] = nums[0];
for (int i = 0; i < nums.length; i++)
{
if(nums[i]>res[0])
{
res[0]=nums[i];
}
if(nums[i]<res[1])
{
res[1]=nums[i];
}
}
return res;
}
}
C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace HelloWorld
{
class Program
{ static void Main(string[] args)
{
int[] nums = { , , , };
int[] res = GetMaxMin(nums);
Console.WriteLine("最大值是:{0} 最小值为:{1}", res[], res[]);
Console.ReadKey(); } private static int[] GetMaxMin(int[] nums)
{
int[] res = new int[];
//设res[0]为最大值,res[1]为最小值
res[] = nums[];
res[] = nums[];
for (int i = ; i < nums.Length; i++)
{
if (nums[i] > res[])
{
res[] = nums[i];
}
if (nums[i] < res[])
{
res[] = nums[i];
}
}
return res;
}
}
}
C#特有的out函数:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace HelloWorld
{
class Program
{ static void Main(string[] args)
{
int[] nums = { , , , };
int max;
int min;
Test(nums, out max, out min);
Console.WriteLine("最大值是:{0} 最小值为:{1}",max, min);
Console.ReadKey(); } private static void Test(int[] nums, out int max, out int min)
{
max = nums[];
min = nums[];
for (int i = ; i < nums.Length; i++)
{
if (nums[i] > max)
{
max = nums[i];
}
if (nums[i] < min)
{
min = nums[i];
}
}
}
}
}
out参数可以返回不同类型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace HelloWorld
{
class Program
{ static void Main(string[] args)
{
int[] nums = { , , , };
int max;
int min;
double avg; //特有:不同类型
Test(nums, out max, out min, out avg);
Console.WriteLine("最大值是:{0} 最小值为:{1} 平均值为:{2}", max, min, avg);
Console.ReadKey(); } private static void Test(int[] nums, out int max, out int min, out double avg)
{
max = nums[];
min = nums[];
int sum = ;
for (int i = ; i < nums.Length; i++)
{
if (nums[i] > max)
{
max = nums[i];
}
if (nums[i] < min)
{
min = nums[i];
}
sum += nums[i];
}
avg = 1.0 * sum / nums.Length;
}
}
}
分析和总结:
1、我们在Main()函数中,调用Test()函数,我们管Main()函数称之为调用者,管Test()函数称之为被调用者。
* 如果被调用者想要得到调用者的值:
1)、传递参数。
2)、使用静态字段来模拟全局变量。
如果调用者想要得到被调用者的值:
1)、返回值
2、不管是实参还是形参,都是在内存中开辟了空间的。
3、方法的功能一定要单一。
GetMax(int n1,int n2)
方法中最忌讳的就是出现提示用户输入的字眼。
4、C#特有的out、ref
值类型的参数,传递的是一份复制。形参的改变不会影响到外面的值。但是c#提供两个关键字 ref和out 让我们可以把值类型当成引用类型来传递。
java不支持ref和out这两个关键字 并且传递的值类似也是一份拷贝。所以改变不会影响外面的值。
1)、out参数。
如果你在一个方法中,返回多个相同类型的值的时候,可以考虑返回一个数组。
但是,如果返回多个不同类型的值的时候,返回数组就不行了,那么这个时候,我们可以考虑使用out参数。
out参数就侧重于在一个方法中可以返回多个不同类型的值。
2)、ref参数
能够将一个变量带入一个方法中进行改变,改变完成后,再讲改变后的值带出方法。ref参数要求在方法外必须为其赋值,而方法内可以不赋值。
谢谢观看!
【05】C#特有的ref、out参数的更多相关文章
- 含有ref out 参数 的方法反射 Emit 与 普通
反射中很多朋友应该屡屡被带有ref out参数的方法折腾 当使用正常反射一个方法时候: 代码如下调用一个后期绑定方法MakeByRefType 就行了 MemberInfo test = typeof ...
- 05:Sysbench压测-innodb_deadlock_detect参数对性能的影响
目录 sysbench压测-innodb_deadlock_detect参数对性能的影响 一.OLTP测试前准备 二.进行OLTP测试 三.测试结果解读: 四.关于测试后的结论: 五.关于测试后的性能 ...
- ref - 按引用传递参数
传递的是引用 在 形参 实参前 加ref
- 05. Matplotlib 1 |图表基本元素| 样式参数| 刻度 注释| 子图
1.Matplotlib简介及图表窗口 Matplotlib → 一个python版的matlab绘图接口,以2D为主,支持python.numpy.pandas基本数据结构,运营高效且有较丰富的图表 ...
- 《Symfony 5全面开发》教程05、http请求的query参数
首先我们删除上节课所下的断点,在Phpstorm底部我们打开debug选项卡.点击这个按钮展开所有的PHP断点,选中之后点击这个删除,然后我们关闭xdebug监听. 回到浏览器刷新页面,当我们的浏览器 ...
- c#.net中参数修饰符ref,out ,params解析
params ============================================================================================= ...
- out参数,ref参数,params参数数组
params参数数组 params关键字可以为方法指定数目可变的参数.params关键字修饰的参数,可以传入任意数目的同类型参数,甚至可以不传入参数. 不过params修饰的参数必须是方法的最后一个参 ...
- ref 参数
当使用ref 作为参数赋值时,ref 得需要初始化,就是在从新定义一下 参数的值,下面有列子: 在控制台中运行如下: //定义一个方法,两个参数 i和a . public static void ge ...
- ref引用类型,数组型参数,out输出参数
ref和out的相同点和不同点 共同点:都是引用传递不同点:ref的参数在调用之前一定要赋值,在方法调用的过程中可以不要赋值. out的参数在调用之前可以不赋值,在方法调用的过程中一定要赋值. ...
- 参数修饰符ref,out ,params的区别
参数修饰符ref,out ,params的区别 C#中有三个关键字-ref,out ,params,可是这三个之间的区别你都明白了吗? 那么我们就来认识一下参数修饰符ref,out ,params吧, ...
随机推荐
- X264-libx264编码库
X264编码库libx264实现真正的视频编解码,该编解码算法是基于块的混合编码技术,即帧内/帧间预测,然后对预测值变换.量化,最后熵编码所得. 编码帧的类型分为I帧(x264_type_i).P帧( ...
- 基于JieBaNet+Lucene.Net实现全文搜索
实现效果: 上一篇文章有附全文搜索结果的设计图,下面截一张开发完成上线后的实图: 基本风格是模仿的百度搜索结果,绿色的分页略显小清新. 目前已采集并创建索引的文章约3W多篇,索引文件不算太大,查询速度 ...
- xshell 远程登陆CentOS7 免密登陆
首先说一下大体的思路: 1. 以密码登陆CentOS系统 2. 配置ssh 3. xshell 生成秘钥 4. 进行免密登陆 软件.设备: xshell(下载地址(免费版),也可以自行百度下载) Ce ...
- 操作Excel模块openpyxl
安装 pip install openpyxl 想要在文件中插入图片文件,需要安装pillow font(字体类):字号.字体颜色.下划线等 fill(填充类):颜色等 border(边框类):设置单 ...
- adb命令之解锁打卡
adb devicesadb shell input keyevent 26 按手机电源键adb shell input swipe 400 1080 40 ...
- Spring(003)-消费返回list的rest服务
通过spring提供的RestTemplate就可以访问rest服务.首先需要创建一个RestTemplate,这个需要手动来创建bean @Configuration public class De ...
- 201871010134-周英杰《面向对象程序设计(java)》第七周学习总结
201871010134-周英杰<面向对象程序设计(java)>第七周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这 ...
- 201871010136-赵艳强《面向对象程序设计(java)》第十四周学习总结
201871010136-赵艳强<面向对象程序设计(java)>第十四周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh ...
- WebSocket协议-基础篇
本篇文章主要讲述以下几点: WebSocket协议出现的背景 WebSocket与HTTP WebSocket API WebSocket协议出现的背景 我们在上网过程中经常用到的是HTTP和HTTP ...
- react服务端渲染框架
客户端渲染 加载一个空的html页面,然后请求一个打包的js文件,然后再客户端执行这个js文件 动态生成html内容然后插入到DOM元素上,在源代码查询中也只能看到空的html文档 没有任何其他内容 ...