C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Info: Whenever an argument to a method is encountered, a token is pushed onto the stack and the new method is invoked.
Steps: In the called method, 3 kinds of memory are allocated: the parameter slots, the local variable slots, and the evaluation stack.
MethodA: This method loads the constant 100 and returns that value to the call site. MethodA is small so it is often inlined.
constMethodB: This accepts a parameter of type boolean. The logic contained in the method is not returned, just the value of the evaluation.
Ternary OperatorMethodC: The integer resulting from the expression is placed on the stack and this single 4-byte value is copied to the call site.
C# program that uses return statement
using System;
class Program
{
static void Main()
{
// Call four methods and store their return values.
int value1 = MethodA();
string value2 = MethodB(true);
string value3 = MethodB(false);
int value4 = MethodC(5, 2);
// Display the results.
Console.WriteLine(value1);
Console.WriteLine(value2);
Console.WriteLine(value3);
Console.WriteLine(value4);
// Invoke a void method.
MethodD();
}
static int MethodA()
{
// This method returns a constant integer.
return 100;
}
static string MethodB(bool flag)
{
// This method returns a string reference based on the flag.
return flag ?
"cat" :
"dog";
}
static int MethodC(int operand1, int operand2)
{
// This method returns an integer based on an expression.
return (operand1 * 10) + operand2;
}
static void MethodD()
{
// This method uses a return statement with no expression (void).
Console.WriteLine("MethodD executed");
return;
}
}
Output
100
cat
dog
52
MethodD executed
Tip: These methods are written more like lambda expressions. We use the lambda operator, and no "return" keyword is specified.
Instead: The result of the expression is treated as a return value. So an invisible "return" is assumed by the C# compiler.
C# program that uses expression-bodied method
using System;
class Program
{
// An expression-bodied method.
static string FormatFancy(string name) => ("Name: " + name.ToUpper());
static void Main(string[] args)
{
// Call the FormatFancy method.
string result = FormatFancy("sam");
Console.WriteLine(result);
}
}
Output
Name: SAM
Here: We introduce a local function called CreateString. It changes the value of "multiplier" on each call.
And: The CreateString method returns a string created with string interpolation syntax.
String InterpolationWarning: This syntax form may be useful, but often a top-level method in a class is more standard and will be easier for others to use.
C# program that uses nested local function
using System;
class Program
{
static void Main()
{
int multiplier = 10;
// This is a local function.
// ... It can access and change local variables in the surrounding scope.
string CreateString(int value)
{
// Use string interpolation.
// ... Modify multiplier after accessing its value.
return $"STRING HAS VALUE: {value * multiplier++}";
}
// Call the local function.
Console.WriteLine(CreateString(5));
Console.WriteLine(CreateString(5));
}
}
Output
STRING HAS VALUE: 50
STRING HAS VALUE: 55
Tip: Like all reference types, the string type is pushed to the stack and it is only 4 or 8 bytes on computers.
String: The string data is never copied when you assign a variable to the result of the method that returns a string.
Note: If the method returns one value, the evaluation stack should have one value of that type when the ret instruction is executed.
And: If the method returns void, the evaluation stack can be empty. Serge Lidin's "Expert .NET 2.0 IL Assembler" describes these things.