首页 技术 正文
技术 2022年11月8日
0 收藏 659 点赞 1,589 浏览 2571 个字

out参数

与c++的引用的对比

out参数可以用来传递方法返回值,与c++中的引用有点像,但是还有有些不同:

– 调用方法的时候必须写out参数

– 调用方法之前必须先分配空间

– 调用方法之前不用先赋值.

– 必须在方法内部对out参数赋值;

下面自己实现一个tryparse函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace out参数
{
class Program
{
public struct Mid
{
public int[] _num;
public string _name;
}
static void Main(string[] args)
{
//写一个方法,求一个数组中的最大值、最小值、总和、平均值。
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
//将要返回的四个值,放到一个数组里返回;但是问题是数组是一组相同类型的数据,所以才能放到一个数组里面。
//如果不同类型的话,只能从参数返回了.这时候又可以用结构封装起来返回;但是这个结构不一定是有意义的,同时是需要做后续操作的.
// Mid mid = GetMaxMinSumAvg(numbers);
// Console.WriteLine("名字是{0}",mid._name);
int max, min, sum;
double avg;
GetTest(numbers, out max, out min, out sum,out avg);
Console.WriteLine("最大值是{0},最小值是{1},总和为{2},平均值为{3}",max,min,sum,avg);
Console.ReadKey(); } /// <summary>
/// 计算一个整数数组的最大值,最小值,平均值,总和,平均值;
/// </summary>
/// <param name="nums">需要计算的那个数组</param>
/// <param name="max">多余返回的最大值</param>
/// <param name="min">多余返回的最小值</param>
/// <param name="sum">对于返回的总和</param>
/// <param name="avg">多余返回的平均值</param>
public static void GetTest(int []nums,out int max,out int min,out int sum,out double avg)
{
//必须在方法内部对out参数赋值;
max = nums[0];
min = nums[0];
sum = nums[0];
for (int i = 1; i < nums.Length; i++)
{
if (nums[i]>max)
{
max = nums[i];
}
if (nums[i]<min)
{
min = nums[i];
}
sum += nums[i];
}
avg = sum * 0.1 / nums.Length; }
/// <summary>
/// 自己实现一个int型的TryParse()函数;
/// </summary>
/// <param name="str"></param>
/// <param name="num"></param>
/// <returns></returns>
public static bool MyTryParse(string str,out int num)
{
try
{
num = int.Parse(str);
}
catch (OverflowException)
{ num = 0;
return false;
}
return true;
}
}
}

ref参数

ref就相当于c++中的引用;比out要更像一点,看它就像是reference的缩写;

注意三点:

– 在调用方法前一定要赋值;

– 调用时一定要写ref;

– 一定要构造空间

下面写一个交换两个变量内容的操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;//ref就相当于c++中的引用;比out要更像一点,out专注返回20年;
namespace ref参数
{
class Program
{
static void Main(string[] args)
{
int n1 = 10;
int n2 = 20;
//注意两点;在调用方法前一定要赋值;一定要写ref;一定要构造空间
Exchange(ref n1, ref n2);
Console.WriteLine("交换后,第一个变量是{0},第二个参数是{1}",n1,n2);
Console.ReadKey(); }
/// <summary>
/// 交换两个变量
/// </summary>
/// <param name="n1">第一个变量</param>
/// <param name="n2">第二个变量</param>
public static void Exchange(ref int n1,ref int n2)
{
n1 = n1 - n2;
n2 = n1 + n2;
n1 = n2 - n1;
}
}
}

params参数

params是可变类型参数,但是它与c++中的变长参数又有不同:

– 将实参列表中跟可变参数数组类型一致的元素都当做数组的元素去处理。

– params可变参数必须是形参列表中的最后一个元素。

– 唯一性:在一个函数里,只能有一个params可变参数

– 调用时有两种传递方式:数组和直接数据传;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Params参数
{
class Program
{
static void Main(string[] args)
{
int [] a={1,2,3};
Test("呵呵",a);
}
public static void Test(string name,params int[] par)
{ }
}
}/*
* param可变参数 可以没有
* 唯一性
* 最后性
* 两种传递方式
* 求任意长度数组的最大值;
*
*/
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289