1、方法的简历
public static 返回值类型 方法名(参数列表)
{
方法体;
}
public: 访问修饰符,公开的,公共的,哪都可以访问。
static: 静态的
返回值类型:不需要写返回值就写void
方法名:Pascal每个单词的首字母大写
参数列表: 完成这个方法所必须提供给这个方法的条件
2、out
public static bool GetNum(int num, out string str)
{
if (num < 100)
{
str = "这个数字小于100";
return false;
}
else
{
str = "这个数字大于于100";
return true;
}
}
static void Main(string[] args)
{
while (true)
{
int a = Convert.ToInt32(Console.ReadLine());
string str;
bool b = GetNum(a, out str);
Console.WriteLine(b.ToString() + str);
}
}
3、ref
public static void GetNum(ref int num)
{
num = num + 5;
}
static void Main(string[] args)
{
while (true)
{
int a = Convert.ToInt32(Console.ReadLine());
GetNum(ref a);
Console.WriteLine(a);
}
}
4、params
public static int GetNum(string name, params int[] score)//params参数必须是最后一个
{
int sum = 0;
for (int i = 0; i < score.Length; i++)
{
sum = sum + score[i];
}
return sum;
}
static void Main(string[] args)
{
// int[] s = { 1, 2, 3, 4, 5 };
int sum = GetNum("zhangsan", 1, 2, 3, 4, 5, 6, 7, 8, 9);
Console.WriteLine(sum);
Console.ReadLine();
}