方法参数
把值传给方法,然后获得返回值。下面介绍的是三种方法(输入,引用,输出参数)引起的输入值与输出结果的变化。
代码参考《展现C#》
using System;
public class SquareSample
{
//通过值传递参数
public int Cals(int len)
{
return len * len;
}
//通过引用参数传递
public void refCals(ref int width)
{
width *= width;
}
//输出参数
public void outCals(int a, out int b)
{
b = a * a;
}
}
class SquareApp
{
public static void Main()
{
SquareSample sq=new SquareSample();
Console.WriteLine(sq.Cals(10));
//变量必须初始化
//定义和调用函数都要加上"ref"
int reftest = 5;
sq.refCals(ref reftest);
Console.WriteLine(reftest);
//不必初始化
int outtest;
sq.outCals(6, out outtest);
Console.WriteLine(outtest);
}
}
代码参考《展现C#》
using System;
public class SquareSample
{
//通过值传递参数
public int Cals(int len)
{
return len * len;
}
//通过引用参数传递
public void refCals(ref int width)
{
width *= width;
}
//输出参数
public void outCals(int a, out int b)
{
b = a * a;
}
}
class SquareApp
{
public static void Main()
{
SquareSample sq=new SquareSample();
Console.WriteLine(sq.Cals(10));
//变量必须初始化
//定义和调用函数都要加上"ref"
int reftest = 5;
sq.refCals(ref reftest);
Console.WriteLine(reftest);
//不必初始化
int outtest;
sq.outCals(6, out outtest);
Console.WriteLine(outtest);
}
}
努力不一定成功,但放弃一定失败!