方法

方法:

 所有程序调式可以设置2个断点就可以使2个断点中间部分不执行)  逐过程与逐语句区别就是遇到方法调用时 逐过程不进而逐语句进;    reflector查看源代码 

功能:用来复用代码的.当我们在一个程序中反复写了同样的代码,那一般情况下我们可以把须要重复写的代码定义在一个方法中,用的时候调用就行了;

语法:

【访问修饰符】【static】 返回值 方法名()

{

方法体;

}

注意:

1)定义在类中

2)有的方法是没有返回值的 如果方法没有返回值则写void

3) 如果方法没有参数()不能省略  ;  方法名开头大写,参数名开头小写;参数名变量名要有意义;

方法调用:对于静态方法,(由static修饰的)

1)类名.方法名();

2)在类中调用本类的方法,可以直接写成:方法名();即(如果在同一类中,直接写名字调用就行) 了.

退出方法:return可以立即退出方法


例:

前后有重复代码

Console.WriteLine("***************");
Console.WriteLine("* 欢迎使用 *");
Console.WriteLine("***************");

int[] scores = { 15, 52, 657, 85, 49, 65, 54, 52, 41, 84 };
for (int i = 0; i < scores.Length - 1; i++)
{
for (int j = 0; j < scores.Length - 1 - i; j++)
{
if (scores[j] < scores[j + 1])
{
int temp = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = temp;
}


}

}


for (int i = 0; i < scores.Length; i++)
{
Console.WriteLine(scores[i]);
}

Console.ReadKey();
Console.Clear();
Console.WriteLine("***************");
Console.WriteLine("* 欢迎使用 *");
Console.WriteLine("***************");
Console.WriteLine("谢谢使用按任意键退出");
Console.ReadKey();


 例2

方法的实际用法演示

ShowUI();

int[] scores = { 15, 52, 657, 85, 49, 65, 54, 52, 41, 84 };
for (int i = 0; i < scores.Length - 1; i++)
{
for (int j = 0; j < scores.Length - 1 - i; j++)
{
if (scores[j] < scores[j + 1])
{
int temp = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = temp;
}


}

}


for (int i = 0; i < scores.Length; i++)
{
Console.WriteLine(scores[i]);
}

Console.ReadKey();
Console.Clear();
ShowUI();
Console.WriteLine("谢谢使用按任意键退出");
Console.ReadKey();

}
/// <summary>
/// 用于显示软件主界面的方法
/// </summary>
public static void ShowUI()
{
Console.WriteLine("***************");
Console.WriteLine("* 欢迎使用 *");
Console.WriteLine("***************");
}

原文地址:https://www.cnblogs.com/swlq/p/5376673.html