Posted by: Cirilo Meggiolaro | 05/11/2009

Tip of the day #209 – .NET Framework 4.0 – Optional Parameters for C#

Welcome to the series of posts about the .NET Framework 4.0.

Let’s start by the first feature that has been available for years on Visual Basic development and now is available for C# programs: Optional parameters on methods.

Take a look on the following code snippet:

public void MyMethod(int a = 123)

The simple method above accepts an integer as parameter. You may have noticed the existence of a value being assigned to the parameter “a”. When a default value is assigned on the method declaration the parameter becomes optional and there is no obligation of passing any value to the method.

Assuming the method calls below, check the results of them:

MyMethod(12); // Output: 12
MyMethod(); // Output: 123


Leave a Reply

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

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


%d bloggers like this: