这是之前在学习.Net框架+ C#语言时候的笔记;
考虑到留在自己电脑本地远远没有存在CSDN云端来的方便与实在。所以整理一下共享出来,方便了自己,也可能帮到某个读者,希望能够有一定的价值。
1,方法的概念
static void Main(string[] args)
{
//不管是形参还是实参,都是在内存中开辟了空间的。
//int a1 = 10;
//int a2 = 20;
int max = GetMax(10, 20);
Console.WriteLine(max);
Console.ReadKey();
}
/// <summary>
/// 计算两个整数之间最的最大值 并且返回最大值
/// </summary>
/// <param name="n1">第一个数</param>
/// <param name="n2">第二个数</param>
/// <returns>返回的最大值</returns>
public static int GetMax(int n1,int n2)//形参
{
int max = n1 > n2 ? n1 : n2;
return max;
}
2,方法的重载
static void Main(string[] args)
{
/*
* 方法的重载
* 概念:方法的重载指的就是方法的名称相同,但是参数不同。
*
* 参数不同,分为两种情况
* 1),如果参数的个数相同,那么参数的类型就不能相同。
* 2),如果参数的类型相同,那么参数的个数就不能相同。
*
* 方法的重载跟返回值没有关系
*
*
*/
}
3,方法的递归
class Program
{
/*
* 方法的递归
* 方法自己调用自己;
* 找出一个文件夹中所有的文件。
*
*/
static void Main(string[] args)
{
TellStory();
Console.ReadKey();
}
public static int i = 0;
public static void TellStory()
{
Console.WriteLine("从前有座山");
Console.WriteLine("山里有座庙");
Console.WriteLine("庙里有个老和尚和小和尚");
Console.WriteLine("有一天,小和尚哭了,老和尚给小和尚讲了一个故事");
i++;
if (i>=10)
{
return;
}
TellStory();
}
4,方法的调用问题
class Program
{
/*
* 1,我们在 Main() 函数中,调用 Test() 函数,我们管 Main() 函数称之为调用者,
* 管 Test() 函数称之为被调用者。
*
* 如果被调用者想要得到调用者的值:
* 1)传递参数。
* 2)使用静态字段来模拟全局变量。
* 如果调用者想要得到被调用者的值:
* 1)返回值
*/
static void Main(string[] args)
{
int a = 3;
Test(a);
Console.WriteLine(a);
Console.ReadKey();
}
public static int Test(int a)
{
a = a + 5;
return a;
}
}
5,方法的练习
class Program
{
static void Main(string[] args)
{
/*
* 1,方法的功能一定要单一 /GetMax(int n1,int n2)
* 2,方法中最忌讳的就是出现提示用户输入的字眼
*/
//读取输入的整数,定义成方法;
//多次调用(如果用户输入的是数字,则返回,否则提示用户重新输入);
}
public static int GetNumber(string s)
{
while (true)
{
try
{
int number = Convert.ToInt32(s);
return number;
}
catch
{
Console.WriteLine("请重新输入");
s = Console.ReadLine();
}
}
}
}
微信公众号:“遇见王川”
编程,理财,英语,欢迎来找我玩。