Methods
Methods
Cheatsheets / Learn C#
Optional Parameters
In C#, methods can be given optional parameters. A // y and z are optional parameters.
parameter is optional if its declaration specifies a
default argument. Methods with an optional parameter
static int AddSomeNumbers(int x, int y =
can be called with or without passing in an argument 3, int z = 2)
for that parameter. If a method is called without passing {
in an argument for the optional parameter, then the
parameter is initialized with its default value.
return x + y + z;
To define an optional parameter, use an equals sign }
after the parameter declaration followed by its default
value.
// Any of the following are valid method
calls.
AddSomeNumbers(1); // Returns 6.
AddSomeNumbers(1, 1); // Returns 4.
AddSomeNumbers(3, 3, 3); // Returns 9.
https://www.codecademy.com/learn/learn-c-sharp/modules/csharp-methods/cheatsheet 1/5
19/01/2024, 13:13 Learn C#: Learn C#: Methods Cheatsheet | Codecademy
Return Keyword
In C#, the return statement can be used to return static int ReturnAValue(int x)
a value from a method back to the method’s caller.
{
When return is invoked, the current method
terminates and control is returned to where the // We return the result of computing x
method was originally called. The value that is returned * 10 back to the caller.
by the method must match the method’s return type,
// Notice how we are returning an int,
which is specified in the method declaration.
which matches the method's return type.
return x * 10;
}
Expression-Bodied Methods
In C#, expression-bodied methods are short methods static int Add(int x, int y)
written using a special concise syntax. A method can
{
only be written in expression body form when the
method body consists of a single statement or return x + y;
expression. If the body is a single expression, then that }
expression is used as the method’s return value.
The general syntax is returnType
funcName(args...) => expression; . static void PrintUpper(string str)
Notice how “fat arrow” notation, => , is used instead {
of curly braces. Also note that the return keyword Console.WriteLine(str.ToUpper());
is not needed, since the expression is implicitly
}
returned.
https://www.codecademy.com/learn/learn-c-sharp/modules/csharp-methods/cheatsheet 2/5
19/01/2024, 13:13 Learn C#: Learn C#: Methods Cheatsheet | Codecademy
Lambda Expressions
// Typical syntax
// (input-parameters) => { <statements> }
There are multiple ways to shorten the concise lambda int[] numbers = { 7, 7, 7, 4, 7 };
expression syntax.
The parameter type can be removed if it can be
inferred. Array.Find(numbers, (int n) => { return n
The parentheses can be removed if there is only != 7; });
one parameter.
As a side note, the usual rules for expression-bodied
methods also apply to lambdas. // The type specifier on `n` can be
inferred based on the array being passed
in and the method body.
Array.Find(numbers, (n) => { return n !=
7; });
https://www.codecademy.com/learn/learn-c-sharp/modules/csharp-methods/cheatsheet 3/5
19/01/2024, 13:13 Learn C#: Learn C#: Methods Cheatsheet | Codecademy
In C#, methods that do not return a value have a // This method has no return value
void return type.
static void DoesNotReturn()
void is not an actual data type like int or
{
string , as it represents the lack of an output or
value. Console.WriteLine("Hi, I don't return
like a bad library borrower.");
}
// This method returns an int
static int ReturnsAnInt()
{
return 2 + 3;
}
Method Declaration
In C#, a method declaration, also known as a method // This is an example of a method header.
header, includes everything about the method other
static int MyMethodName(int parameter1,
than the method’s body. The method declaration
includes: string parameter2) {
the method name // Method body goes here...
parameter types
parameter order
}
parameter names
return type
optional modifiers
A method declaration you’ve seen often is the
declaration for the Main method (note there is more
than one valid Main declaration):
static void Main(string[] args)
https://www.codecademy.com/learn/learn-c-sharp/modules/csharp-methods/cheatsheet 4/5
19/01/2024, 13:13 Learn C#: Learn C#: Methods Cheatsheet | Codecademy
Out Parameters
return can only return one value. When multiple // f1, f2, and f3 are out parameters, so
values are needed, out parameters can be used.
they must be prefixed with `out`.
out parameters are prefixed with out in the
method header. When called, the argument for each
static void GetFavoriteFoods(out string
out parameter must be a variable prefixed with f1, out string f2, out string f3)
out . {
The out parameters become aliases for the variables // Notice how we are assigning values
that were passed in. So, we can assign values to the
to the parameters instead of using
parameters, and they will persist on the variables we
passed in after the method terminates. `return`.
f1 = "Sushi";
f2 = "Pizza";
f3 = "Hamburgers";
}
Print Share
https://www.codecademy.com/learn/learn-c-sharp/modules/csharp-methods/cheatsheet 5/5