【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吧, ...
随机推荐
- Struts2 Action的3种创建方式
Action是Strut2的核心内容,相当于Servlet,用于处理业务. Action是一个Java类,直接新建Java类即可. Action有3种实现方式. 1.使用POJO,设置成员变量,写对应 ...
- [PHP] 广度优先搜索匹配网站所有链接
<?php define('PRE_DOMAIN','www'); define('DOMAIN','sina.com.cn'); define('PROTOCOL','https'); def ...
- LSTM——长短时记忆网络
LSTM(Long Short-term Memory),长短时记忆网络是1997年Hochreiter和Schmidhuber为了解决预测位置与相关信息之间的间隔增大或者复杂语言场景中,有用信息间隔 ...
- C语言之++和--
#include<stdio.h> int main(void) { int a; a = ; printf("a++ = %d\n", a++); printf(&q ...
- luoguP1198 [JSOI2008]最大数
https://www.luogu.org/problem/P1198 update!!! 经过老师的讲解,惊人的发现这题有用更简单数据结构维护的解法,而越简单的数据结构(如果能够用的话),越好(实现 ...
- python 函数,模块知识点运用示例
给定验证码长度n,生成随机验证码,验证码由数字.字母组成(参考chr()内置方法) # 给定验证码长度n,生成随机验证码,验证码由数字.字母组成(参考chr()内置方法) # 第33-126号(共94 ...
- python27期day07:基础数据类型补充、循环删除的坑、二次编码、作业题。
1.求最大位数bit_length: a = 10 #8421 1010print(a.bit_length())结果:42.capitalize首字母变大写: s = "alex" ...
- 旅游网dao层
# 注册功能 # 1.根据用户名查询用户 select * from tab_user where username=#{username} # 2.插入注册用户信息 insert into tab_ ...
- 查看ubuntu已安装软件
查看安装的所有软件 dpkg -l “dpkg ”是“Debian Packager ”的简写.为 “Debian” 专门开发的套件管理系统,方便软件的安装.更新及移除.所有源自“Debian”的“L ...
- hadoop 输入路径用正则表达式被默认处理为多个参数的问题
运行命令 hadoop jar wordcount.jar com.WordCount /inpath/*{beijing,shanghai,guangzhou}* /outpath/ ...