Return value and return operator
A method can return a value, some result. In the example above, two methods were defined that had the void type. Methods with this type do not return any value. They just perform some actions.
But methods can also return some value. For this purpose, the return operator is used, followed by the return value:
return return value;
For example, let's define a method that returns a value of the string type:
string GetMessage()
{
return “Hello”
}
The GetMessage method has the string type, so it must return a string. That's why the return operator is used in the body of the method, after which the returned string is specified.
At the same time, methods that have any type other than void as their return type must necessarily use the return operator to return the value. For example, the following method definition is incorrect:
string GetMessage()
{
Console.WriteLine(“Hello”);
}
Also, there must be a correspondence between the return type of the method and the return value after the return operator. For example, in the following case the return type is string, but the method returns a number (type int), so this method definition is incorrect:
string GetMessage()
{
return 3; // Error! The method should return a string, not a number
}
The result of methods that return a value we can assign to variables or use in other ways in the program:
string GetMessage()
{
return “Hello”
}
string message = GetMessage(); // get the result of the method into the variable message
Console.WriteLine(message); // Hello
The GetMessage() method returns a value of the string type. So we can assign this value to some variable of string type: string message = GetMessage();
Or even pass it as a value to a parameter of another method:
string GetMessage()
{
return “Hello”
}
void PrintMessage(string message)
{
Console.WriteLine(message);
}
PrintMessage(GetMessage());
In the PrintMessage(GetMessage()) call, the GetMessage() method is called first and its result is passed to the message parameter of the PrintMessage method
After the return operator, you can also specify complex expressions or calls to other methods that return a certain result. For example, let's define a method that returns the sum of numbers:
int Sum(int x, int y)
{
return x + y;
}
int result = Sum(10, 15); // 25
Console.WriteLine(result); // 25
Console.WriteLine(Sum(5, 6)); // 11
The Sum() method is of int type, so it must return a value of int type - an integer. Therefore, the return operator is used in the body of the method, followed by the return number (in this case, the result of the sum of the x and y variables).
Abbreviated version of methods with result
We can also abbreviate methods that return a value:
string GetMessage()
{
return “hello”
}
similar to the following method:
string GetMessage() => “hello”;
And the method
int Sum(int x, int y)
{
return x + y;
}
is similar to the following method:
int Sum(int x, int y) => x + y;
Exiting the method
The return operator not only returns a value, but also exits the method. Therefore, it should be defined after the other instructions. For example:
string GetHello()
{
return “Hello”;
Console.WriteLine(“After return”);
}
From the syntax point of view, this method is correct, but its Console.WriteLine(“After return”) instruction is meaningless - it will never be executed, because before its execution the return operator will return the value and exit the method.
However, we can use the return operator in methods with void type. In this case, no return value is placed after the return operator (because the method does not return anything). A typical situation is to exit the method depending on certain conditions:
void PrintPerson(string name, int age)
{
if(age > 120 || age < 1)
{
Console.WriteLine(“Invalid age”);
return;
}
Console.WriteLine($“Name: {name} Age: {age}”);
}
PrintPerson(“Tom”, 37); // Name: Tom Age: 37
PrintPerson(“Dunkan”, 1234); // Invalid age
Here, the PrintPerson() method takes the user's name and age as parameters. However, in the method, we first check if the age matches some range (less than 120 and greater than 0). If the age is out of this range, we print a message about invalid age and exit the method with the return operator. After that the method finishes its work.
However, if the age is correct, we output the user information to the console. Console output:
Name: Tom Age: 37
Invalid age