-
若我们在Main()函数中,调用Test()函数,我们管Main()函数叫 调用者,管Test()函数叫 被调用者。
-
如果 被调用者 想要得到 调用者 的值:
- 传递参数。
- 定义全局变量,使用静态字段来模拟全局变量。
public static int num = 2;
-
如果 调用者 想要得到 被调用者 的值:
- 返回值
1、out参数
如果你在一个方法中,返回多个相同类型的值的时候,可以考虑返回一个数组。但是,如果返回多个不同类型的值的时候,返回数组就不行了,那么这个时候,我们可以考虑使用out参数。
out参数就侧重于在一个方法中可以返回多个不同类型的值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FunctionParameters
{
class Program
{
static void Main(string[] args)
{
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// 调用1
Console.WriteLine("调用1:");
int[] res = GetMaxMinSumAvg(numbers);
Console.WriteLine("最大值是{0},最小值是{1},总和是{2},平均值是{3}", res[0], res[1], res[2], res[3]);
Console.ReadKey();
// 调用2
Console.WriteLine("调用2:");
int max;
int min;
int sum;
int avg;
bool b;
string s;
GetMaxMinSumAvg(numbers, out max, out min, out sum, out avg, out b, out s);
Console.WriteLine("最大值是{0},最小值是{1},总和是{2},平均值是{3}", max, min, sum, avg);
Console.WriteLine(b);
Console.WriteLine(s);
Console.ReadKey();
// 调用3
Console.WriteLine("调用3:");
int num;
//bool buer = int.TryParse("3.14a", out num); // 也可
bool buer = MyTryParse("3.14a", out num);
Console.WriteLine(num);
Console.WriteLine(buer);
Console.ReadKey();
}
/// <summary>
/// 计算一个整数数组的最大值、最小值、平均值、总和
/// </summary>
/// <param name="nums">需要计算的数组</param>
/// <returns>计算结果,res[0] 最大值 res[1]最小值 res[2]总和 res[3]平均值</returns>
public static int[] GetMaxMinSumAvg(int[] nums)
{
int[] res = new int[4];
res[0] = nums[0]; //max
res[1] = nums[0]; //min
res[2] = 0; //sum
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];
}
res[2] += nums[i];
}
res[3] = res[2] / nums.Length;
return res;
}
/// <summary>
/// 计算一个整数数组的最大值、最小值、平均值、总和
/// </summary>
/// <param name="nums">需要计算的数组</param>
/// <param name="max">多余返回的最大值</param>
/// <param name="min">多余返回的最小值</param>
/// <param name="sum">多余返回的总和</param>
/// <param name="avg">多余返回的平均值</param>
/// <param name="b">多余返回的布尔值</param>
/// <param name="str">多余返回的字符串</param>
public static void GetMaxMinSumAvg(int[] nums, out int max, out int min, out int sum, out int avg, out bool b, out string str)
{
// out参数要求在方法的内部必须为其赋值
max = nums[0];
min = nums[0];
sum = 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;
b = true;
str = "计算完成。";
}
/// <summary>
/// 演示out用法:字符串转int
/// </summary>
public static bool MyTryParse(string s, out int result)
{
result = 0;
try
{
result = Convert.ToInt32(s);
return true;
}
catch
{
return false;
}
}
}
}
2、ref参数
能够将一个变量带入一个方法中进行改变,改变完成后,再讲改变后的值带出方法。
ref参数要求在方法外必须为其赋值,而方法内可以不赋值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FunctionParameters
{
class Program
{
static void Main(string[] args)
{
int n1 = 2;
int n2 = 3;
Console.WriteLine("n1={0},n2={1}", n1,n2);
ExchangeValue(ref n1, ref n2);
Console.WriteLine("n1={0},n2={1}", n1, n2);
Console.ReadKey();
}
/// <summary>
/// 交换两个数字的值
/// </summary>
public static void ExchangeValue(ref int n1, ref int n2)
{
// 方法1
//int temp = n1;
//n1 = n2;
//n2 = temp;
// 方法2
n1 = n1 - n2;
n2 = n1 + n2;
n1 = n2 - n1;
}
}
}
3、params可变参数
将实参列表中跟可变参数数组类型一致的元素都当做数组的元素去处理。
params可变参数必须是形参列表中的最后一个元素。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FunctionParameters
{
class Program
{
static void Main(string[] args)
{
Test("张三", "2020001", 100, 100, 100);
int[] s = { 99, 88, 77 };
Test("张三", "2020001", s);
Console.ReadKey();
}
public static void Test(string name, string id, params int[] score)
{
int sum = 0;
for (int i = 0; i < score.Length; i++)
{
sum += score[i];
}
Console.WriteLine("{0}的学号是{1},这次考试的总成绩是{2}", name, id, sum);
}
}
}