Learn C#: Methods
Learn to call and create methods: a fundamental building block in C# development.
StartKey Concepts
Review core concepts you need to learn to master this subject
Optional Parameters
Variables Inside Methods
Void Return Type
Method Declaration
Return Keyword
Out Parameters
Expression-Bodied Methods
Lambda Expressions
Optional Parameters
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.
- 1Imagine 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 …
- 2You’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…
- 3Like 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…
- 4Up 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…
- 5Remember 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…
- 6One 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…
- 7To 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…
- 8Say 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…
- 9Say 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…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory