C# 4.0 Parameterless Functions

Integrating flexibility into class behavior.

When designing class behavior, knowing ahead of time how that function will need to be used is sometimes difficult.
For example, when defining the function signature how do you know in what order the parameters should be arranged and what if that makes a difference to the user?

The C# language now overcomes this challenge with a new feature to the 4.0 specification called “parameterless functions” which allow each parameter to be defined with a default value.
This default value assignment allows the user to pass arguments in any order by simply specifying with each argument the parameter to which it will be used.

To illustrate this feature, I’ll begin with a simple Person class.

To enable this feature within the constructor during object instantiation, I only need to provide a default value for each parameter.

2-parameterless-functions

To leverage the flexibility of passing arguments in any desired order, I simply define each argument’s parameter when calling the function.
Notice in this example, I’m instantiating two Person objects while passing arguments in two different orders.
This flexibility is particularly useful when the data being consumed might have fields at different orders, such as with text files.

3-parameterless-functions

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s