C# Return Statement with Examples

  Keywords : C# return statement is useful to return a value from methods with examples, C# return is a jump statement to return the control to the calling method with examples

Here we will learn return statement in c# with examples and how to use return statement in c# to terminate the execution of the method and returns control to the calling method with examples.

C# Return Statement

In c#, Return statement is used to terminate the execution of 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 a some value from the other methods and we can omit the usage of return statement in our methods by using void as a return type.

Syntax of C# Return Statement

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

 

return return_val; 

If you observe 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 object based on our requirements.

 

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

C# Return Statement Example

Following is the example of using return statement in 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 Any Key to Exit..");

            Console.ReadLine();

        }

        public static int SumofNumbers(int a, int b)

        {

            int x = a + b;

            return x;

        }

    }

}

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

Output of C# Return Statement Example 

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

 

C# Return Statement Example Result

 

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