有无参数方法
输出10次整数
// 无参数方法
public static void ProgramMothod(){
for (int i = 0;i<10;i++)
{
Console.WriteLine(1)
}
}
// 有参数方法
public static void ProgamMothod(int a){
for (int i = 0; i < 10; i++)
{
Console.WriteLine(a)
}
}
有无返回值
void 无返回值 代表结果无出口
// 有返回值,必须用 return (关键字) 把结果传出去
public static int ProgramMothod(int a){
Console.WriteLine(a);
return a;
}
/ 调用
int retrunNumber = ProgramMothod(300);
//无返回值 void 无返回值,代表方法结果无出口
public stat void PorgeamMothod1(int a ){
Console.WriteLine(a-1);
}
/ 调用
ProgramMothod1(100);
参数传递
ref
// 参数传递形式
public static void PorgramMothod(int a,int b){
// 交换a和b的值,如果交换之后 不影响外部 tempA 和 tempB的值 引用参数传递
int c;
c=a;
a=b;
b=c;
}
// 调用
int tempA = 100;
int tempB = 200;
ProgramMothod(tempA,tempB);
Console.WriteLine(tempA);
Console.WriteLine(tempB);
// 引用参数 如果交换之后 影响外部tempA和tempB的值 引用参数传递
public static void ProgramMothod1(ref int a,ref int b)
{
//交换a和b的值 如果交换之后 不影响外部tempA和tempB的值 就是值参数传递
int c;
c = a;
a = b;
b = c;
}
/// 调用方法
int tempC = 300;
int tempD = 400;
ProgramMothod1(ref tempC,ref tempD);
Console.WriteLine(tempC);
Console.WriteLine(tempD);
// 输出参数
//1.是对于方法返回值的补充。return 可用于函数中返回一个值 输出参数可以返回多个值
//2关键字out: out输出参数在方法中 必须被使用,且和return保持一致
//3其他方法引用参数相似
public static void ProgramMothod3(out int a, out int b,out int c)
{
b = 20;
c = 30;
a=1000;
}
// 调用方法
int tempB1;
int tempC2;
int tempA1;
ProgramMothod3(out tempA1,out tempB1,out tempC2);
Console.WriteLine(tempA1);
Console.WriteLine(tempB1);
Console.WriteLine(tempC2);