return (Riferimenti per C#)return (C# Reference)
L'istruzione return
termina l'esecuzione del metodo in cui viene visualizzata e restituisce il controllo al metodo di chiamata.The return
statement terminates execution of the method in which it appears and returns control to the calling method. Può restituire anche un valore facoltativo.It can also return an optional value. Se il metodo è un tipo void
, l'istruzione return
può essere omessa.If the method is a void
type, the return
statement can be omitted.
Se l'istruzione return è all'interno di un blocco try
, il blocco finally
, se presente, verrà eseguito prima che il controllo venga restituito al metodo di chiamata.If the return statement is inside a try
block, the finally
block, if one exists, will be executed before control returns to the calling method.
EsempioExample
Nell'esempio seguente il metodo CalculateArea()
restituisce la variabile locale area
come valore double
.In the following example, the method CalculateArea()
returns the local variable area
as a double
value.
class ReturnTest
{
static double CalculateArea(int r)
{
double area = r * r * Math.PI;
return area;
}
static void Main()
{
int radius = 5;
double result = CalculateArea(radius);
Console.WriteLine("The area is {0:0.00}", result);
// Keep the console open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
// Output: The area is 78.54
Specifiche del linguaggio C#C# language specification
Per ulteriori informazioni, vedere la specifica del linguaggio C#.For more information, see the C# Language Specification. La specifica del linguaggio costituisce il riferimento ufficiale principale per la sintassi e l'uso di C#.The language specification is the definitive source for C# syntax and usage.