1,在Main函数内,将函数返回值赋值给一个变量即可,然后在函数里用return语句将结果传到Main方法里面的变量。
例如:
using System;
public class Rectangle
{
double height, width, area;
public Rectangle(double _height, double _width)
{
this.height = _height;
this.width = _width;
}
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
}
} |