Home » Blog » C# 4.0 – Optional Parameters, Default Values, and Named Parameters

Archive for the ‘Visual Studio’ Category
C# 4.0 – Optional Parameters, Default Values, and Named Parameters
Monday, March 22nd, 2010

Parameters

Parameters are simply values that can be passed into a method. A method specifies what parameters, along with their type, it accepts and expects. Traditionally in C#, when we want to accept different sets of parameters or set default values, we would overload methods and supply the information by chaining the calls. This can be messy and can cause a lot of variations of a method. Keeping track of default values can also become a challenge.


static void Main(string[] args)
{
   CreatePerson("Marius");
   CreatePerson("Marius", 24);
   CreatePerson("Marius", 24, "USA");
}

static void CreatePerson(string Name)
{
    CreatePerson(Name, 24, "USA");
}

static void CreatePerson(string Name, int Age)
{
    CreatePerson(Name, Age, "USA");
}

static void CreatePerson(string Name, int Age, string Location)
{
    //Logic...
}

Default Values & Optional Parameters in C# 4.0
In order to deal with the default value issue, C# 4.0 has introduced default values. By assigning a default to a parameter, we don’t require the value to be passed in which automatically makes it optional.

static void Main(string[] args)
{
    CreatePerson("Marius");
    CreatePerson("Marius", 24);
    CreatePerson("Marius", 24, "USA");
}

static void CreatePerson(string Name, int Age = 20, string Location= "USA")
{
    //Logic...
}

Named Parameters

When you have a method that has multiple optional parameters and you want to provide the value of only one, you have to use named parameters. These are quite simply parameters formatted as [Parameter Name]: Value

static void Main(string[] args)
{
    CreatePerson("Marius", BirthPlace: "Other Location");
    CreatePerson("Marius", Age:26);
    CreatePerson("Marius", BirthPlace: "Other Location", Age: 26);
}

static void CreatePerson(string Name, int Age = 24, string BirthPlace = "USA")
{
    //Logic...
}

Visual Studio & Conditions

The only conditions placed on the developer is that optional parameters are to be declared at the end of the method arguments, after all of the full parameters have been declared. This means that when calling the methods, all full parameters must be passed in like a normal method, and then the named parameters can be given for the optional default parameters in any order.

Visual Studio 2010 adds supports for these new language features very nicely. As you can see below, the default values for the optional parameters are shown, and intellisense supports the selection of the named paramaters as expected.

VS 2010 Optional Parameters

 

Other Thoughts
As with each iteration of the C# language, Optional Parameters, Named Parameters, and Default Values give even more control to the C# developer. These new features will save a lot of overloading and method chaining, and will undoubtebly save a lot of developers from the “factory method” value instatiation.

HAPPY CODING!

Tortoise SVN Global Ignore Pattern for Visual Studio 2008 w/ ReSharper
Thursday, February 25th, 2010

Source/Version/Revision Control

How many times have you spent countless hours writing code just to realize you’ve either overwritten or accidentally deleted your files? How many times have you been working with a team of developers just to realize your coworker has overwritten your code?

This has unfortunately happened to the best of us at one point or another. This issue usually prompts us to look for a way to easily backup our code and share it between multiple developers. On our journey, we find many different flavors of source/revision control. Dependent upon your preference there are multiple options available, some free, some purchased. For more information on source control visit Wikipedia.

The Problem

For those of you that have chosen Tortoise SVN, a windows shell integrated CVS source control solution, you may find it unnecessary to save files or folders created by plug-ins and visual studio. These files can be very annoying as they usually change every time you open up your solution, or build your application. A popular plug-in for Visual Studio that creates extra files and folders is ReSharper. I will focus on removing the files created by this plug-in.

As you can see, ReSharper folders and files are available to add to the source control by default.

The Solution

In order to tell Tortoise SVN to ignore the files and folders created by ReSharper and to not list them every time we commit our files, we have to add the extensions/folders to Tortoise SVN’s global ignore pattern text box.

Ignore pattern to ignore ReSharper Created files:

**.ReSharper** **\_ReSharper.**

Ignore pattern to ignore Visual Studio Files which shouldn’t be source controlled:

*\bin* *\obj* *.suo *.user

Combined Ignore pattern to ignore both Visual Studio and ReSharper files:

*\bin* *\obj* *.suo *.user **.ReSharper** **\_ReSharper.**


Implementation

Adding any of these “ignore” rules is simple. First identify the rule you want to add; I recommend the combined rule, and then:

1. Right click on any of your repositories, hover over TortoiseSVN menu option, and click on the Settings sub link.

2. Add the rule you have chosen to the global ignore pattern and remember to click “OK”.

That’s it! Now when you try to commit your changes, you won’t see any of those annoying extra files!

HAPPY CODING!