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参数的更多相关文章

  1. 含有ref out 参数 的方法反射 Emit 与 普通

    反射中很多朋友应该屡屡被带有ref out参数的方法折腾 当使用正常反射一个方法时候: 代码如下调用一个后期绑定方法MakeByRefType 就行了 MemberInfo test = typeof ...

  2. 05:Sysbench压测-innodb_deadlock_detect参数对性能的影响

    目录 sysbench压测-innodb_deadlock_detect参数对性能的影响 一.OLTP测试前准备 二.进行OLTP测试 三.测试结果解读: 四.关于测试后的结论: 五.关于测试后的性能 ...

  3. ref - 按引用传递参数

    传递的是引用 在 形参 实参前 加ref

  4. 05. Matplotlib 1 |图表基本元素| 样式参数| 刻度 注释| 子图

    1.Matplotlib简介及图表窗口 Matplotlib → 一个python版的matlab绘图接口,以2D为主,支持python.numpy.pandas基本数据结构,运营高效且有较丰富的图表 ...

  5. 《Symfony 5全面开发》教程05、http请求的query参数

    首先我们删除上节课所下的断点,在Phpstorm底部我们打开debug选项卡.点击这个按钮展开所有的PHP断点,选中之后点击这个删除,然后我们关闭xdebug监听. 回到浏览器刷新页面,当我们的浏览器 ...

  6. c#.net中参数修饰符ref,out ,params解析

    params ============================================================================================= ...

  7. out参数,ref参数,params参数数组

    params参数数组 params关键字可以为方法指定数目可变的参数.params关键字修饰的参数,可以传入任意数目的同类型参数,甚至可以不传入参数. 不过params修饰的参数必须是方法的最后一个参 ...

  8. ref 参数

    当使用ref 作为参数赋值时,ref 得需要初始化,就是在从新定义一下 参数的值,下面有列子: 在控制台中运行如下: //定义一个方法,两个参数 i和a . public static void ge ...

  9. ref引用类型,数组型参数,out输出参数

    ref和out的相同点和不同点 共同点:都是引用传递不同点:ref的参数在调用之前一定要赋值,在方法调用的过程中可以不要赋值.    out的参数在调用之前可以不赋值,在方法调用的过程中一定要赋值. ...

  10. 参数修饰符ref,out ,params的区别

    参数修饰符ref,out ,params的区别 C#中有三个关键字-ref,out ,params,可是这三个之间的区别你都明白了吗? 那么我们就来认识一下参数修饰符ref,out ,params吧, ...

随机推荐

  1. 北京地铁出行线路规划系统项目总结(Java+Flask+Vue实现)

    北京地铁出行线路规划系统项目总结 GitHub仓库地址:https://github.com/KeadinZhou/SE-Subway Demo地址:http://10.66.2.161:8080/ ...

  2. bootstrap基础样式学习(二)——栅格

    (1)最外层必须使用容器 div.container或 div.container-fluid (2)容器可以放置任何内容,若想使用栅格系统必须用 div.row div.container > ...

  3. Netty粘包问题(六)

    netty使用tcp/ip协议传输数据,而tcp/ip协议是类似水流一样的数据传输方法.多次访问的时候可能出现粘包的问题,解决这种问题的方式有如下几种. 一.定长数据流 二.特殊结束符 三.

  4. Python前言之编程语言

    编程语言分类(语言) ​ 编程语言是用来和计算机进行交互的,计算机只认识0和1. 机器语言(低级语言) 直接和硬件进行交互 用0和1和计算机进行沟通 缺点:开发效率低 优点:执行效率高 汇编语言 直接 ...

  5. xgboost:

    https://www.zybuluo.com/Dounm/note/1031900 GBDT算法详解 http://mlnote.com/2016/10/05/a-guide-to-xgboost- ...

  6. flutter环境配置window10

    第一步,配置git环境,这个作为前端的都是会的,如果你不会,去问度娘去 第二步,配置java的开发环境,这里建议下载jdk为1.8版本的,我最初使用的是如下图的jdk版本,后面和flutter版本不一 ...

  7. 「总结」插头$dp$

    集中做完了插头$dp$ 写一下题解. 一开始学的时候还是挺蒙的. 不过后来站在轮廓线$dp$的角度上来看就简单多了. 其实就是一种联通性$dp$,只不过情况比较多而已了. 本来转移方式有两种.逐行和逐 ...

  8. NOI2016优秀的拆分

    一种想法是枚举分割位置, 然后考虑前面部分有多少种可行的AA拆分方式, 后面部分有多少种可行的BB拆分方式, 然后乘法原理即可 那么问题是如何快速求出合法方案 解法是首先枚举长度len, 然后将序列分 ...

  9. 字段加密实践(django-fernet-fields)

    一.fernet介绍 Fernet 用于django模型字段对称加密,使用 crytography 库. 官网帮助文档 1.先决条件 django-fernet-fields 支持Django 1.8 ...

  10. 前端Vue项目——登录页面实现

    一.geetest滑动验证 geetest官方文档地址:https://docs.geetest.com/ 产品——极速验证:基于深度学习的人机识别应用.极验「行为验证」是一项可以帮助你的网站与APP ...