您的位置:首页 > 编程语言 > C#

C#基础(8)——高级参数out、ref、params

2017-10-26 19:05 507 查看

1、out

一个方法返回不同类型的值,如int、string、char等,需要out。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{

class Program
{
static void Main(string[] args)
{
int[] n = new int[11];
int re_max;
int re_min;
double re_sum;
double re_avg;
Random r = new Random();
for (int i = 0; i < n.Length; i++)
{
n[i] = r.Next(0, 12);
Console.Write("{0}  ", n[i]);
}
Test(n, out re_max, out re_min, out re_sum, out re_avg);
//真正将re_avg保留两位
Console.WriteLine("\n返回的最大值:{0},最小值:{1},总和:{2},均值:{3:0.000}", re_max, re_min, re_sum, re_avg);
re_avg = Convert.ToDouble(re_avg.ToString("0.00"));
Console.WriteLine("真正re_avg保留两位:{0}", re_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 Test(int[] nums, out int max, out int min, out double sum, out double avg)
{
max = int.MinValue;
min = int.MaxValue;
sum = 0;
avg = 0;
for (int i = 0; i < nums.Length; i++)
{
if (nums[i] > max)
{
max = nums[i];
}
if (nums[i] < min)
{
min = nums[i];
}
sum += nums[i];
avg = sum / nums.Length;
}
}
}
}




使用多余out,进行用户的登录

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{

class Program
{
static void Main(string[] args)
{
string msg;//不需要赋值,因为函数有返回值
bool islogin = false;
do
{
Console.WriteLine("用户名:");
string name = Console.ReadLine();
Console.WriteLine("密码:");
string passwd = Console.ReadLine();
islogin = Is_Login(name, passwd, out msg);
Console.WriteLine(msg);
} while (!islogin);
Console.ReadKey();
}

public static bool Is_Login(string logname, string pwd, out string msg)
{
if (logname == "admin" && pwd == "123")
{
msg = "登录成功,欢迎回来!";
return true;
}
else if (logname != "admin")//只能选中其中一种情况
{
msg = "登录名错误!";
return false;
}
else if (pwd != "123")
{
msg = "密码错误!";
return false;
}
else
{
msg = "未知错误...";
return false;
}
}
}
}




out参数

int.TryParse的模仿:





2、ref

将实参代入方法中进行修改,返回修改后了的值,不需要return一个定义的形参;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{

class Program
{
static void Main(string[] args)
{
double salary;
Console.WriteLine("请输入当前薪水:");
salary  = Convert.ToDouble(Console.ReadLine());
TestRef(ref salary);//没有声明类型接收
Console.WriteLine("加薪后:{0}", salary);
Console.ReadKey();
}

public static void TestRef(ref double s)//没有返回类型
{
s += 500;
}
}
}




交换两个数字

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{

class Program
{
static void Main(string[] args)
{
int num1 = 10;
int num2 = 20;
TestRef(ref num1, ref num2);
Console.WriteLine("交换后:num1={0}  num2={1}",num1,num2);
Console.ReadKey();
}

/// <summary>
/// 用于不另外开辟变量的情况下,交换两个变量值
/// </summary>
/// <param name="n1">第一个变量</param>
/// <param name="n2">第二个变量</param>
public static void TestRef(ref int n1,ref int n2)//没有返回类型
{
n1 = n1 - n2;
n2 = n1 + n2;
n1 = n2 - n1;
}
}
}




判断两个数字的大小:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{

class Program
{
public static int i = 0;
static void Main(string[] args)
{
Console.WriteLine("第一个数字:");
int n1 = GetInt(Console.ReadLine());
Console.WriteLine("第二个数字:");
int n2 = GetInt(Console.ReadLine());
JudgeNumber(ref n1, ref n2);

Console.WriteLine("最后的两个数:{0},{1}", n1, n2);
Console.ReadKey();
}

public static int GetInt(string s)
{
while (true)
{
try
{

int number = Convert.ToInt32(s);
return number;
}
catch
{
Console.WriteLine("输入有误!请重新输入:");
s = Console.ReadLine();
}
}
}
public static void JudgeNumber(ref int n1, ref int n2)
{
while (true)
{
if (n1 < n2)
{
return;
}
else
{
Console.WriteLine("再次第一个数字:");
n1 = GetInt(Console.ReadLine());
Console.WriteLine("再次第二个数字:");
n2 = GetInt(Console.ReadLine());
}
}
}

}
}




params

params可变参数,且参数为最后的定义,用于减少声明的参数,如改进前:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{

class Program
{
static void Main(string[] args)
{
int[] array = { 1, 2, 3, 4, 5 };
Tets("张三", array);
Console.ReadKey();
}

public static void Tets(string name, int[] grade)
{
int sum = 0;
for (int i = 0; i < grade.Length; i++)
{
sum += grade[i];
}
Console.WriteLine("{0}的总成绩是:{1}", name, sum);
}

}
}


改进后:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{

class Program
{
static void Main(string[] args)
{
//int[] array = { 1, 2, 3, 4, 5 };
Tets("张三", 1, 2, 3, 4, 5);
Console.ReadKey();
}

public static void Tets(string name, params int[] grade)
{
int sum = 0;
for (int i = 0; i < grade.Length; i++)
{
sum += grade[i];
}
Console.WriteLine("{0}的总成绩是:{1}", name, sum);
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: