C# Return Statement

C# Return Statement

The C# jump statements are break, continue, goto, return, and throw.

1 - C# Return Statement

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.

 

2 - C# Return Statement example

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:

C# Return Statement - XDevSpace