The return statement terminates execution of the method in which it appears and returns control to the calling method. It can also return the value of the optional expression. If the method is of the type void, the return statement can be omitted. The return statement takes the form:
return [expression];
where:
In the following example, the method A()
returns the variable Area
as a double value.
using System;
class ReturnTest {
static double CalculateArea(int r) {
double area;
area = r*r*Math.PI;
return area;
}
public static void Main() {
int radius = 5;
Console.WriteLine("The area is {0:0.00}", CalculateArea(radius));
}
}
The area is 78.54