一:方法/函数
方法就是一大段代码,给这一段代码起个名字,这就是方法名,每个方法都封装了其对应的功能,当你需要使用这个功能的时候直接调用该方法即可
访问权限修饰符 static 返回值类型 方法名(形参列表)
{
//方法体 -> 编写功能代码
}
访问权限修饰符:是否有权限调用该方法。
static:静态的。
返回值类型:方法执行完成后返回的结果类型。如果不需要返回结果使用void关键字。
方法名:自定义名称,大驼峰命名规范。
参数:方法运行时需要的数据。可以有参数,也可以没有参数。
定义在方法中的变量是局部变量
参数类型为值类型:值类型变量存储的为实际的值,作为参数传递时,传递的依然为实际的值
参数类型为引用类型:引用类型变量存储的为地址,作为参数传递时,传递的依然为地址
参数类型为string类型:string存储的值不允许被改变
ref:在方法的参数中使用ref,将值类型的值传递变为引用传递。
如:static void Main(string\[\] args)
{
int i = 10;
Test(ref i);
Console.WriteLine(i); //200
Console.ReadKey();
}
public static void Test(ref int i)
{
i = 200;
}
paeams:params主要是在声明方法时,参数个数不确定时使用,使用时,需注意以下几点:
- 参数数组必须是一维数组。
- 不允许将params修饰符与ref修饰符组合起来使用。
- 与参数数组对应的实参可以是同一类型的数组名,也可以是任意多个与该数组的元素属于同一类型的变量。
- 形参列表中有多个参数,params必须在最后一个。
如:static void Main(string\[\] args)
{
Test(1,2,3);
Console.ReadKey();
}
public static void Test(params int\[\] ints)
{
}
out:out一般用在函数需要有多个返回值的场景,out底层则是内部为外部变量赋值。
接收返回值的变量必须由out修饰
有多少种返回值,就可以声明多少个out修饰的返回值类型
tryParse方法:int.tryParse()方法:将字符串类型转换为int类型。
方法源码:
csharp
public static bool TryParse(string s, out int result){//...}
方法特点:
- 转换成功,返回true,将成功转换的值由result返回。
- 转换失败,返回false,result赋值为0返回。
示例:
csharp
int result;
bool v = int.TryParse("abc", out result);
//bool v = int.TryParse("abc", out int result);
Console.WriteLine(v);
Console.WriteLine(result);
Console.ReadKey();
二:练习:

实现代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lianxi1
{
internal class Program
{
static void Main(string\[\] args)
{
//1.编写一个方法,判断一个数是否为奇数,如果是奇数返回true,否则返回false。
//while (true)
//{
// Console.WriteLine("请输入一个数:");
// int num = Convert.ToInt32(Console.ReadLine());
// if (IsOdd(num))
// {
// Console.WriteLine("输入q结束");
// string input = Console.ReadLine();
// if (input == "q")
// {
// break;
// }
// }
//}
//2.编写一个方法,接收一个字符串数组,将数组中的元素拼接成一个字符串,元素之间用"|"分隔。
//string[] mingXin = new string[] { "梅西","卡卡","郑大世"};
//string result = JoinString(mingXin);
//Console.WriteLine(result);
//3.编写一个方法,接收一个整数数组,返回数组中的最大值和最小值。
//int max, min;
//int[] arr = new int[100];
//int num = Convert.ToInt32(Console.ReadLine());
//for (int i = 0; i < num; i++)
//{
// arr[i] = Convert.ToInt32(Console.ReadLine());
//}
//FindNumber(arr, out max, out min, num);
//Console.WriteLine($"最大值:{max}, 最小值:{min}");
//4.编写一个方法,接收一个整数数组,对数组进行排序,并返回排序后的数组。
//int[] arr = new int[] { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 };
//Sort(arr);
//5.编写一个方法,接收一个整数成绩,根据成绩返回对应的等级,成绩在80分以上为"优",60-79分为"良",60分以下为"差"。
//Console.WriteLine("请输入您的成绩:");
//int score = Convert.ToInt32(Console.ReadLine());
//Console.WriteLine(GetLevel(score));
//6.编写一个方法,接收一个字符串数组,将数组中的元素进行反转,并返回反转后的数组。a
string[] guoJia = new string[] { "中国","韩国", "美国", "日本"};
Reverse(guoJia);
Console.ReadKey();
}
public static void Reverse(string[] guoJia)
{
for (int i = 0; i < guoJia.Length / 2; i++)
{
string temp = guoJia[i];
guoJia[i] = guoJia[guoJia.Length - 1 - i];
guoJia[guoJia.Length - 1 - i] = temp;
}
foreach (var item in guoJia)
{
Console.Write(item + " ");
}
}
public static string GetLevel(int score)
{
string result;
if(score >= 80)
{
result = "优";
}
else if(score >= 60)
{
result = "良";
}
else
{
result = "差";
}
return result;
}
public static void Sort(int[] arr)
{
for (int i = 0; i < arr.Length - 1; i++)
{
for (int j = 0; j < arr.Length - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
foreach (var item in arr)
{
Console.Write(item + " ");
}
}
public static void FindNumber(int[] arr, out int max, out int min, int num)
{
max = arr[0];
min = arr[0];
for (int i = 1; i < num; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
if (arr[i] < min)
{
min = arr[i];
}
}
}
static string JoinString(string[] mingXin)
{
string result = "";
for (int i = 0; i < mingXin.Length; i++)
{
result += mingXin[i];
if (i < mingXin.Length - 1)
{
result += "|";
}
}
return result;
}
static bool IsOdd(int num)
{
if(num % 2 != 0) return true;
else return false;
}
}
}