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.
Following is the syntax of using a return statement in the c# programming language.
If you observe the above syntax, we used a return keyword as the 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.
Following is the example of using a return statement in the c# programming language.
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.
When we run the above c# program, we will get the result below.
This is how we can use the return statement in the c# programming language based on our requirements.