<visibility> <return type> <name>(<parameters>)
{
<function code>
}
visibility是可选的,默认是private
return type是返回值类型,可以是void
栗子:
publicvoid DoStuff()
{ Console.WriteLine("I'm doing something...");}
publicint AddNumbers(int number1,int number2)
{ int result= number1+ number2;
if(result>10)
{ return result; }
}
输出还是20,因为传进去的是个copy,主函数里面的值不会改变!!!
namespace value_ref
{
class Program
{
static void AddFive(int number)
{
number = number + 5;
}
static void Main(string[] args)
{
int number = 20;
AddFive(number);
Console.WriteLine(number);
Console.ReadKey();
}
}
}
输出25 加入ref关键字
namespace value_ref
{
class Program
{
static void AddFive(ref int number)
{
number = number + 5;
}
static void Main(string[] args)
{
int number = 20;
AddFive(ref number);
Console.WriteLine(number);
Console.ReadKey();
}
}
}
===============params===============================
static void Main(string[] args)
{
GreetPersons(0);
GreetPersons(25, "John", "Jane", "Tarzan");
Console.ReadKey();
}
static void GreetPersons(int someUnusedParameter, params string[] names)
{
foreach (string name in names)
Console.WriteLine("Hello, " + name);
}