Skip to content

Named arguments C# 4.0readability

Allow callers of a method to specify the name of each argument before the value.

Sometimes passing values to methods can be quite unreadable. Booleans, integers and other constants aren't always clear without digging into the signature.

Named arguments lets you optionally put the parameter name next to the value.

Additionally when a method has Optional arguments C# 4.0 they are used to specify which arguments are being passed without having to specify default values or nulls.

Code

C#
GetCustomer(id: 1, eager: true, depth:3);

// (Show use of optional arguments too)
Customer getCustomer(int id, bool eager, Context context = null, int depth = 0)
{
    // ...
}
C#
GetCustomer(1, true, null, 3);

Customer getCustomer(int id, bool eager, Context context, int depth)
{
    // ...
}

More information