一、方法的定义和调用
定义一个打印方法,在主方法中调用该方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 方法_定义和调用
{
class Program
{
static void Main(string[] args)//主方法
{
Print();//调用打印方法
}
//定义打印方法
static void Print()
{
Console.Write("===============打印方法===============");
}
}
}
控制台输出:
二、方法的调用和参数传递
在程序中,除了主方法外,还定义了三个方法,分别是:合并数组方法(ArrMerge)、数组排序方法(ArrBubbleSort)和打印数组方法(PrintArr)。这三个方法都定义了整型数组的参数,只是形参的个数有所不同,此外,ArrMerge方法和ArrBubbleSort方法都有返回值,其类型为int型一维数组,而PrintArr没有返回值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 方法调用和参数传递
{
class Program
{
//把两个数合并成一个数组,并对合并的数组排序
//数组1:{3,5,7,2,1}
//数组2:{9,12,4}
static void Main(string[] args)
{
int[] arr1 = new int[] { 3, 5, 7, 2, 1 };//数组1
int[] arr2 = new int[] { 9, 12, 4 };//数组2
int arr1_len = arr1.Length;//数组1长度
int arr2_len = arr2.Length;//数组2长度
int[] arr3 = new int[arr1_len + arr2_len];//定义数组3(合并数组)
arr3 = ArrMerge(arr1, arr2);//合并数组arr1,arr2
arr3 = ArrBubbleSort(arr3); //对arr3排序
PrintArr(arr3);//打印arr3
}
//合并两个一维数组
static int[] ArrMerge(int[] a1, int[] a2)
{
int[] a3 = new int[a1.Length + a2.Length];
for (int i = 0; i < a3.Length; i++)
{
if (i < a1.Length)
{
a3[i] = a1[i];//合并数组a1
}
else
{
a3[i] = a2[i - a1.Length];//合并数组a2
}
}
return a3;
}
//对数组排序(冒泡排序法)
static int[] ArrBubbleSort(int[] a)
{
for (int j = 0; j < a.Length - 1; j++)//共a.Length-1次冒泡
{
//一次冒泡
for (int i = 0; i < a.Length - 1 - j; i++)//每次比较a.Length-1-j次
{
if (a[i] > a[i + 1])
{
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
return a;//返回数组a
}
//打印数组
static void PrintArr(int[] a)
{
for (int i = 0; i < a.Length; i++)
{
Console.Write(a[i] + " ");
}
}
}
}
控制台输出:
三、方法重载
这里选择重载求数组最大值Max方法作为方法重载的例子,方法重载后,参数类型或者参数个数和返回值类型应该改变。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 方法重载
{
class Program
{
//求int型数组最大值
static int Max(int[] a)
{
int max = a[0];//定义最大值
for (int i = 1; i < a.Length; i++)
{
max = max > a[i] ? max : a[i];//使用三目运算符?:来比较相邻元素,得到最大值并重新赋值给max
}
return max;
}
//求float型数组最大值(重载Max方法)
static float Max(float[] a)
{
float max = a[0];
for (int i = 1; i < a.Length; i++)
{
max = max > a[i] ? max : a[i];
}
return max;
}
static void Main(string[] args)
{
int[] a1 = { 1, 2, 3, 6, 2, 5 };
float[] a2 = { 1.1f, 5.2f, 3f, 0.2f };
Console.WriteLine(Max(a1));//调用int型Max方法
Console.WriteLine(Max(a2));//调用float型Max方法
}
}
}
控制台输出:
四、方法中参数修饰符ref和out
ref:引用参数,输出前变量必须赋值
out:输出参数,返回前必须在方法中赋值,用在函数可能返回多个值的时候
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 参数修饰符_ref和out
{
class Program
{
//ref:引用参数,传递前变量必须赋值
//out:输出参数,返回前在方法中必须赋值,用在函数可能返回多个值的时候
static void Main(string[] args)
{
int a = 5;
int b;
Change(ref a);//调用Change方法
Change1(out b);//调用Change1方法
Console.WriteLine(a);
Console.WriteLine(b);
}
//含有ref引用参数的Change方法
static void Change(ref int a)
{
a = 10;
}
//含有out输出参数的Change1方法
static void Change1(out int a)
{
a = 100;
}
}
}
控制台输出: