Posted by: Cirilo Meggiolaro | 05/12/2009

Tip of the day #210 – .NET Framework 4.0 – Named Parameters

A new feature available on the .NET framework 4.0 is called Named Parameters. The idea of the named parameters is to invoke operations without passing the parameters in the regular sequence as we’ve been developing but to name the parameters.

A similar feature was added to the .NET framework 3.0 that allows us to initialize classes by using named properties. For more details about class initializer, check the Tip #72.

How to

The method declaration does not change at all but the way how we invoke it has a different syntax. Assuming we have the following method:

public void MyMethod(int customerId, DateTime shipDate) { }

The syntax to invoke the method using named parameter is MethodName(parameterName: Value);

For our sample method we can use named parameters in a similar way than the next code:

MyMethod(shipDate: DateTime.Now, customerId: 1234);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: