Chapter 10

Improving Productivity with Named and Optional Parameters

IN THIS CHAPTER

Bullet Distinguishing between named and optional parameters

Bullet Using optional parameters

Bullet Declaring and using output parameters

Bullet Implementing reference return types

Parameters, as you probably remember, are the inputs to methods. They’re the values that appear as part of the method’s signature. When the method returns a value (it doesn’t always do so), the parameters provide the data required to generate the output value. Sometimes, the return values are parameters (out parameters), which confuses things.

In ancient versions of C# and most C-derived languages, parameters can't be optional (oddly enough, you find some examples of this ancient code lurking about online just waiting to make you feel hindered). Instead of making parameters optional, you are required to make a separate overload for every version of the method you expect your users to need. This pattern works well, but there are some problems that are explored in this chapter.

C# 4.0 and above have optional parameters. Optional parameters are parameters ...

Get C# 10.0 All-in-One For Dummies now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.