Breaking News

Using return statements in C#

If you want a method to return information (means its return type is not void). You must write a return statement inside the method. You can do this by using the keyword return followed by an expression that calculates the returned value and a semi colon.
The return value type must be the same as the type specified by the function i.e. if the function return type is an int, the return statement must return an integer value; otherwise your program will not be compiled successfully.
int addNumber(int firstNumber, int secondNumber)
//… return firstNumber + secondNumber;
The return statement should be at the end of your method because it causes the method to finish. Any statement after the return statement are not executed (although the compiler warns you about this problem if you write any statements after return statement).
If you don’t want to return information (means its return type is void), you write the keyword return , immediately followed by a semicolon to cause an immediate exit from the method.
void showDetails(string name)
//Display the name … return;
Note: if your method doesn’t return anything, you can omit the return statement, because the method finishes automatically when execution arrives at the closing curly brace at the end of the method.
Method Calling
You can call a method by name with its parameter to ask it to perform action. If the method required information (as specified by its parameters), you must supply the information requested. If the method returns information (as specified by the return type), you should arrange to capture this information. The syntax of a C# method call is as follows:
methodName ( parameterList );

1. the method name must exactly the name of the method which you are calling. Always remember, C# is a case-sensitive language.

2. You must include the parentheses in every method call, even when calling a method has no arguments.


Check Also

Exceptions In C#

System.Exception is the base class for all other exceptions. Below this level are the System.ApplicationException …