#18 – C# return Statement

The return statement is used to exit a method of a class, returning control to the caller of the method.

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.

using System;


Console.WriteLine($"Get value: {Test()}");

int Test()
{
    try
    {
        return 2 * 5;
    }
    catch (System.Exception)
    {
        throw;
    }
    finally
    {
        Console.WriteLine("This is from finally.");
    }
}

Output:

This is from finally.
Get value: 10

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.