C# Return Statement with Examples

In c#, the return statement is useful to terminate the execution of the method in which it appears and returns the control back to the calling method.

 

Generally, in c# the return statement is useful whenever we want to get some value from the other methods. We can omit the usage of return statements in our methods by using void as a return type.

Syntax of C# Return Statement

Following is the syntax of using a return statement in the c# programming language.

 

return return_val;

If you observe the above syntax, we used a return keyword as return type, and the value parameter return_val is used to return the value. The return_val parameter value can be either string or integer or array or list of the object based on our requirements.

 

Now we will see how to use a return statement in the c# programming language with examples.

C# Return Statement Example

Following is the example of using a return statement in the c# programming language.

 

using System;

namespace Tutlane
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 10, j = 20, result = 0;
            result = SumofNumbers(i, j);
            Console.WriteLine("Result: {0}", result);
            Console.WriteLine("Press Enter Key to Exit..");
            Console.ReadLine();
        }
        public static int SumofNumbers(int a, int b)
        {
             int x = a + b;
             return x;
        }
    }
}

If you observe the above code, we used a return statement in the SumofNumbers method. Whenever we call the SumofNumbers method, it will execute that method and return the resultant value using the return statement.

Output of C# Return Statement Example 

When we run the above c# program, we will get the result as shown below.

 

C# Return Statement Example Result

 

This is how we can use the return statement in the c# programming language based on our requirements.