C# 7.0-7.3 gives users the liberty to use named and optional arguments when calling methods.
When entering arguments for a method’s parameters, the user typically needs to match the position of the parameters in the method declaration. However, with the help of named arguments, it is possible to send arguments to a method in any order.
The user can send a mix of named arguments and positional arguments, as long as the positional and named arguments are in the correct places.
Named arguments improve the readability of the code by presenting what each argument means.
The following example demonstrates how named arguments can be used in C# 7.0-7.3:
using System; class NamedExample { static void Main(string[] args) { // call the method in a normal way StudentDetails("Sammy", "123", 20); // CASE 1 // call method using named arguments StudentDetails( marks: 20, name: "Sammy", id: "123"); StudentDetails( id: "123", marks: 20, name: "Sammy"); // CASE 2 /// Mix of positional and named arguments StudentDetails(name: "Sammy", "123", 20); StudentDetails("Sammy", id: "123", 20); StudentDetails(name: "Sammy", "123", marks: 20); } static void StudentDetails(string name, string id, int marks) { Console.WriteLine($"Student: {name}, ID#: {id}, Marks: {marks}"); } }
Student: Sammy, ID#: 123, Marks: 20
Student: Sammy, ID#: 123, Marks: 20
Student: Sammy, ID#: 123, Marks: 20
Student: Sammy, ID#: 123, Marks: 20
Student: Sammy, ID#: 123, Marks: 20
Student: Sammy, ID#: 123, Marks: 20
The program first calls the StudentDetails
method in a typical way, and enters the arguments for the parameters in the order in which they are defined.
In case 1, the program mixes the order of arguments but uses named arguments. The compiler can identify which argument belongs to which parameter because the program mentions the names of the parameters along with the arguments.
In case 2, the program uses a mix of named and positional arguments. This works as long as the named and positional arguments are in their correct places.
The following cases are invalid:
StudentDetails("123", 20, name:"Sammy");
StudentDetails("Sammy", 20, id: "123");
This case is only available in C# 7.2 onwards.
A method can contain required and optional arguments.
The user must define optional arguments after the required arguments, and each optional argument must have a default value to be used if the user does not provide a value. A default value can be any of the following:
new ValType()
, where ValType is a value type, such as an enum or a struct.default(ValType)
, where ValType is a value type.The following example demonstrates how to use optional arguments:
using System; class NamedExample { static void Main(string[] args) { // call the method in a normal way StudentDetails("Sammy", "123", 20); // CASE 1 // call method using optionalarguments StudentDetails("Sammy"); StudentDetails("Sammy", "123"); StudentDetails("Sammy", "123", 20); // CASE 2 // call the method using named argument StudentDetails("Sammy", marks: 20); } static void StudentDetails(string name, string id = "234", int marks = 45) { Console.WriteLine($"Student: {name}, ID#: {id}, Marks: {marks}"); } }
Student: Sammy, ID#: 123, Marks: 20
Student: Sammy, ID#: 234, Marks: 45
Student: Sammy, ID#: 123, Marks: 45
Student: Sammy, ID#: 123, Marks: 20
Student: Sammy, ID#: 234, Marks: 20
In case 1, the program calls the SudentDetails
method three times. The first call only sends the required argument, so the default values of the last two arguments are printed. Similarly, the method prints the default value of the last argument in the second call, and the provided values for all three arguments in the third call.
In case 2, the program demonstrates how to provide the value for the second optional argument without providing the value for the first optional argument. This is only possible through named arguments.
A statement like this to achieve case 2 would therefore be invalid:
StudentDetails("Sammy", , 20);
RELATED TAGS
CONTRIBUTOR
View all Courses