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.

Method Calls and Input
Lesson 1 of 3
  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…

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