public double GetArea()
{
area = height * width; return area;
}
public void Show()
{
Console.WriteLine("该长方形面积是{0}", area);
}
}
class Progarm
{
static void Main()
{
Rectangle r = new Rectangle(10, 12);
r.GetArea();
r.Show();
}
}
2,通过引用传递值类型,也就是说传递值(变量)的引用。
例如:
class PassingValByRef
{
static void SquareIt(ref int x)
{
x *= x;
System.Console.WriteLine("The value inside the method: {0}", x);
}
static void Main()
{
int n = 5;
System.Console.WriteLine("The value before calling the method: {0}", n);//输出5
SquareIt(ref n); // Passing the variable by reference.
System.Console.WriteLine("The value after calling the method: {0}", n);//输出25
}
}作者: 刘旺 时间: 2012-7-23 14:48
用out 和ref 但是要知道out参数只进不出,ref参数有进有出
static void TestRefAndOut()
{
string s1 = "Good Luck!";
TestRef(ref s1);
Console.WriteLine(s1);//output: Hello World!
}
static void TestRef(ref string str)
{
str = "Hello World!";
}