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

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