Key Concepts

Review core concepts you need to learn to master this subject

Optional Parameters

// y and z are optional parameters. static int AddSomeNumbers(int x, int y = 3, int z = 2) { return x + y + z; } // Any of the following are valid method calls. AddSomeNumbers(1); // Returns 6. AddSomeNumbers(1, 1); // Returns 4. AddSomeNumbers(3, 3, 3); // Returns 9.

In C#, methods can be given optional parameters. A parameter is optional if its declaration specifies a default argument. Methods with an optional parameter can be called with or without passing in an argument 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.

To define an optional parameter, use an equals sign after the parameter declaration followed by its default value.

Variables Inside Methods

// y and z are optional parameters. static int AddSomeNumbers(int x, int y = 3, int z = 2) { return x + y + z; } // Any of the following are valid method calls. AddSomeNumbers(1); // Returns 6. AddSomeNumbers(1, 1); // Returns 4. AddSomeNumbers(3, 3, 3); // Returns 9.

Parameters and variables declared inside of a method cannot be used outside of the method’s body. Attempting to do so will cause an error when compiling the program!

Void Return Type

// y and z are optional parameters. static int AddSomeNumbers(int x, int y = 3, int z = 2) { return x + y + z; } // Any of the following are valid method calls. AddSomeNumbers(1); // Returns 6. AddSomeNumbers(1, 1); // Returns 4. AddSomeNumbers(3, 3, 3); // Returns 9.

In C#, methods that do not return a value have a void return type.

void is not an actual data type like int or string, as it represents the lack of an output or value.

Method Declaration

// y and z are optional parameters. static int AddSomeNumbers(int x, int y = 3, int z = 2) { return x + y + z; } // Any of the following are valid method calls. AddSomeNumbers(1); // Returns 6. AddSomeNumbers(1, 1); // Returns 4. AddSomeNumbers(3, 3, 3); // Returns 9.

In C#, a method declaration, also known as a method header, includes everything about the method other than the method’s body. The method declaration includes:

  • the method name
  • 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)

Return Keyword

// y and z are optional parameters. static int AddSomeNumbers(int x, int y = 3, int z = 2) { return x + y + z; } // Any of the following are valid method calls. AddSomeNumbers(1); // Returns 6. AddSomeNumbers(1, 1); // Returns 4. AddSomeNumbers(3, 3, 3); // Returns 9.

In C#, the return statement can be used to return 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 method was originally called. The value that is returned by the method must match the method’s return type, which is specified in the method declaration.

Out Parameters

// y and z are optional parameters. static int AddSomeNumbers(int x, int y = 3, int z = 2) { return x + y + z; } // Any of the following are valid method calls. AddSomeNumbers(1); // Returns 6. AddSomeNumbers(1, 1); // Returns 4. AddSomeNumbers(3, 3, 3); // Returns 9.

return can only return one value. When multiple values are needed, out parameters can be used.

out parameters are prefixed with out in the method header. When called, the argument for each out parameter must be a variable prefixed with out.

The out parameters become aliases for the variables that were passed in. So, we can assign values to the parameters, and they will persist on the variables we passed in after the method terminates.

Expression-Bodied Methods

// y and z are optional parameters. static int AddSomeNumbers(int x, int y = 3, int z = 2) { return x + y + z; } // Any of the following are valid method calls. AddSomeNumbers(1); // Returns 6. AddSomeNumbers(1, 1); // Returns 4. AddSomeNumbers(3, 3, 3); // Returns 9.

In C#, expression-bodied methods are short methods 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 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;. Notice how “fat arrow” notation, =>, is used instead of curly braces. Also note that the return keyword is not needed, since the expression is implicitly returned.

Lambda Expressions

// y and z are optional parameters. static int AddSomeNumbers(int x, int y = 3, int z = 2) { return x + y + z; } // Any of the following are valid method calls. AddSomeNumbers(1); // Returns 6. AddSomeNumbers(1, 1); // Returns 4. AddSomeNumbers(3, 3, 3); // Returns 9.

A lambda expression is a block of code that is treated like any other value or expression. It can be passed into methods, stored in variables, and created inside methods.

In particular, lambda expressions are useful for creating anonymous methods, methods with no name, to be passed into methods that require method arguments. Their concise syntax is more elegant than declaring a regular method when they are being used as one off method arguments.

Shorter Lambda Expressions

// y and z are optional parameters. static int AddSomeNumbers(int x, int y = 3, int z = 2) { return x + y + z; } // Any of the following are valid method calls. AddSomeNumbers(1); // Returns 6. AddSomeNumbers(1, 1); // Returns 4. AddSomeNumbers(3, 3, 3); // Returns 9.

There are multiple ways to shorten the concise lambda expression syntax.

  • The parameter type can be removed if it can be inferred.
  • The parentheses can be removed if there is only one parameter.

As a side note, the usual rules for expression-bodied methods also apply to lambdas.

Chevron Left Icon
Method Calls and Input
Lesson 1 of 3
Chevron Right Icon
  1. 1
    Imagine making a hamburger: 1. Place the bread down 2. Add the burger patty 3. Add the pickles 4. Place the bread on top What if you had to say each step every time you ordered a hamburger? It’s …
  2. 2
    You’ve been using methods since you started learning C#! Commands like Console.WriteLine() and Math.Min() are methods. Each method has a different behavior: The first method prints something to t…
  3. 3
    Like a math function or a factory machine, a method takes input and returns output. We’ve just seen how input works (arguments). Let’s see how output works. When a method returns a value, it e…
  4. 4
    Up until now, you’ve been calling built-in methods: methods that are available whenever you use C#. Sometimes you need a custom method for your specific program. In that case, you’ll need to define…
  5. 5
    Remember calling methods with arguments, like Math.Min(3, 4)? Methods that you define can use arguments as well, making them more versatile and useful. While we are defining our method, we don’t k…
  6. 6
    One thing to watch for with parameters: they can only be used inside their method! static void YourMethodName(string message) { Console.WriteLine(message); } Console.WriteLine(message); // causes…
  7. 7
    To make our functions even more flexible, we can make certain parameters optional. If someone calls your method without all the parameters, the method will assign a default value to those missing…
  8. 8
    Say your method has lots of optional parameters, but you only want to specify one when you call it. In this example, your method has five optional parameters: static void YourMethodName(int a = 0…
  9. 9
    Say you want to use Math.Round(), a built-in method. You go to the Microsoft documentation to learn how to use it, and find at least…
  10. 10
    You learned a lot this lesson: congrats on finishing! Here’s what you’ve covered: Call a method with its name and parentheses: VisitPlanets(); Store a method’s returned value in a variable: do…
  1. 1
    What’s the outcome of calling a method? Sometimes a message is printed to the console: Console.WriteLine(“Hello World!”); Sometimes a value is returned: Math.Floor(15.6); // Returns 15 Sometimes…
  2. 2
    The basic way to return values from a method is to use a return statement! (A well-constructed programming language shouldn’t have a lot of surprises.) Let’s start with an example in the below c…
  3. 3
    As we mentioned before, we don’t like surprises — they lead to mistakes. So, when we call a method, we’d like to know what type of value will be returned. This is done in the method definitio…
  4. 4
    A method can only return one value, but sometimes you need to output two pieces of information. For example the Int32.TryParse() method tries to parse its input as an integer. If it can, it ret…
  5. 5
    We can use out parameters in our own methods as well. In this example, Yell() converts phrase to uppercase and sets a boolean variable to true: static string Yell(string phrase, out bool wasYellCa…
  6. 6
    As with return, out is a very useful keyword, but it can lead to errors if used incorrectly. Here are two common ones: This error means that the out parameter needs to be assigned a value within t…
  7. 7
    Congrats on finishing! You can now use and define methods with output. Here’s what else you’ve learned in this lesson: Methods return values with the return keyword. Every method has a return …
  1. 1
    Good developers don’t work too hard. We all push ourselves to get better at coding, but when something seems difficult or repetitive, there’s usually a better way to do it! Methods are a good exa…
  2. 2
    Expression-bodied definitions are the first “shortcut” for writing methods. They’re great for writing one-line methods, like this one: bool IsEven(int num) { return num % 2 == 0; } We can re…
  3. 3
    Before we get into the next shortcut, we need to understand how methods are passed to other methods as arguments. This is possible and sometimes necessary in C#. For example, say we need to check…
  4. 4
    The next shortcut, lambda expressions, are great for situations when you need to pass a method as an argument. In the past exercise, we used IsEven() to check that an even value exists in the ar…
  5. 5
    Time to put on our detective caps: using deductive reasoning, we can make our lambda expression even shorter. Here’s what we have to start: bool hasEvenNumbers = Array.Exists(numbers, (int num) =>…
  6. 6
    Well done! We learned two shortcuts for defining methods: Expression-bodied definitions can be used for one-line method bodies. bool isEven(int num) => num % 2 == 0; Lambda expressions can b…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo