The C# jump statements are break
, continue
, goto
, return
, and throw
.
The return
statement exits the method.
decimal AMethod (decimal d) {
decimal p = d * 100m;
return p;
}
A return
statement can appear anywhere in a method.
return statement would exit the method and program will run the first statement after the method.
For a method which has a non-void return type and the return statement must return an expression of the method's return type.
If the method is void, you can just use return;
to exit a method.
A return statement can appear anywhere in a method except in a finally block.
The following Add() method returns decimal type:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(1234M);
Console.WriteLine(Add(1234M));
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
static decimal Add(decimal d)
{
decimal p = d + 10100m;
return p; // Return to the calling method with value
}
}
}
Output: