How Can You Use Optional Parameters in C#

C# Optional parameters allows us to define methods with optional arguments that are not required to pass by the caller. Learn how to pass optional parameters in C#.

C# allows you to define a method, constructor, indexer, or delegate with parameters that are required or optional. The caller code does not need to pass the value of optional parameters as an argument. When declared, each optional parameter has a default value. If no argument is passed by the caller code, the default value is used.
 
In this article, learn how to define optional parameters in C# methods and how to call a method with optional arguments. 
 
Some important notes about optional parameters:A default value must be a constant expression, an expression of the form new ValType(), or an expression of the form default(ValType).
  • Optional parameters must be defined at the end of the parameter list, after any required parameters. 
Define a Method with Optional Parameters
 
The following code snippet defines a class with some properties and a method, CalculateBookRoyalties. This method calculate author book royalties for the books sold. The input parameters are book price, quantity, and rate. The method has one optional parameter, rate. The default value of rate is 12. That means, when no value is passed as an argument, the default value = 12 will be used.
  1. class Author  
  2. {  
  3. // Auto-Initialized properties  
  4. public string Name { getset; }  
  5. public string Book { getset; }  
  6. public double Price { getset; }  
  7. // Calculate author's book royalties  
  8. public double CalculateBookRoyalties(double price, int quantity, Int16 rate = 12)  
  9. {  
  10. return (quantity * price * rate) / 100;  
  11. }  
  12. }  
Call a method with optional parameters
 
The following code calls CalculateBookRoyalties method, with two arguments only. The default value of the rate argument = 12 is used in the calculation. 
  1. // Create an Author  
  2. Author author = new Author { Name = "Mahesh Chand", Book = "Apress", Price = 49.95 };  
  3. // Call a method and do not pass optional argument  
  4. double royalty = author.CalculateBookRoyalties(50, 100);  
Here is the complete C# code example: 
  1. using System;  
  2. namespace OptionalParametersSample  
  3. {  
  4. class Program  
  5. {  
  6. static void Main(string[] args)  
  7. {  
  8. // Create an Author  
  9. Author author = new Author { Name = "Mahesh Chand", Book = "Apress", Price = 49.95 };  
  10. // Call a method and do not pass optional argument  
  11. double royalty = author.CalculateBookRoyalties(50, 100);  
  12. Console.WriteLine($"Author Royalties: $ {royalty}");  
  13. Console.ReadKey();  
  14. }  
  15. }  
  16. class Author  
  17. {  
  18. // Auto-Initialized properties  
  19. public string Name { getset; }  
  20. public string Book { getset; }  
  21. public double Price { getset; }  
  22. // Calculate author's book royalties  
  23. public double CalculateBookRoyalties(double price, int quantity, Int16 rate = 12)  
  24. {  
  25. return (quantity * price * rate) / 100;  
  26. }  
  27. }  
  28. }  
Note: Both named and optional arguments can be used with methods, indexers, constructors, and delegates.
 
The output of the above code looks like this:
 
 Optional Parameters In CSharp
 
Learn more about  here: Named Arguments and Optional Parameters