Optional parameters and named arguments

Another way to simplify methods is to make parameters optional. You make a parameter optional by assigning a default value inside the method parameter list. Optional parameters must always come last in the list of parameters.

There is one exception to optional parameters always coming last. C# has a params keyword that allows you to pass a comma-separated list of parameters of any length as an array. You can read about params at the following link: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/params

You will now create a method with three optional parameters.

Inside the Person class, add the following code:

public string OptionalParameters(string command = "Run!", double ...

Get C# 7.1 and .NET Core 2.0 – Modern Cross-Platform Development - Third Edition 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.