Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#

What are non-trailing named arguments in C# 7.0-7.3?

Ayesha Naeem

C# 7.0-7.3 gives users the liberty to use named and optional arguments when calling methods.

Named arguments

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.

Example

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}");
    }
}

Output

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.

Case 1

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.

Case 2

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.

Optional arguments

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:

  • A constant expression.
  • An expression like new ValType(), where ValType is a value type, such as an enum or a struct.
  • An expression like default(ValType), where ValType is a value type.

Example

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}");
    }
}

Output

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

Case 1

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.

Case 2

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

c#

CONTRIBUTOR

Ayesha Naeem
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring