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. 多线程之callable学习

    最近在看多线程方面的内容,注意到java中原来除了Runnable和Thread之外还有Callable的方式实现多线程,并且Callable还能得到子线程的返回值,这是前面两种方式所不具有的. Ca ...

  2. spring boot jsp里面的静态资源访问不到解决办法

    闲着没事写的小Demo 用到了jsp页面,里面有些静态资源, springboot 默认的静态资源的值有四个:Default: classpath:/META-INF/resources/,class ...

  3. STM32芯片命名规则

  4. 微信公众号token验证失败

    我用的是python3+,而官网给的例子是python2的写法.问题就在python版本不同. 下面是截取官方的实例代码的一部分 list = [token, timestamp, nonce] li ...

  5. 201871010136-赵艳强《面向对象程序设计(java)》第十二周学习总结

    201871010136-赵艳强<面向对象程序设计(java)>第十二周学习总结   项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh ...

  6. 第七周scrum会议

    本周会议地点依旧 会议照片: 本周内容: 讨论了进行中的难点 我们正在分析图书馆首页的网页结构以及各种跳转的请求以及链接,为爬虫的实现奠定基础. flask框架我们也遇到了很多问题,正在进行官方文档的 ...

  7. 【转】前后端分离的项目如何部署发布到Linux

    前后端分离的项目如何部署发布到Linux 前期准备 1.服务器的基本配置信息2.本机远程连接服务器的工具(xshell.xftp或者mobaXterm等等,看你自己喜欢) 第一步:部署环境 1.安装j ...

  8. <console>:14: error: not found: value spark import spark.implicits.

    启动 ./spark-shell 出现问题 启动 hadoop, 并创建,解决 hadoop fs -mkdir /directory 解决了

  9. matlab的plot3()函数、mesh()函数和surf()函数

    1.plot3()函数 例1:绘制一条空间折线. x=[0.2,1.8,2.5]; y=[1.3,2.8,1.1]; z=[0.4,1.2,1.6]; figure(1);plot3(x,y,z); ...

  10. SQL-select常用语句

    1.全套装备 select [select选项] 字段列表[字段别名]/* from 数据源[where 条件子句] [group by条件子句] [having 子句] [order by 子句] ...