C# 4.0 Optional Parameters and C# 4.0 Named Parameters

I have to say I absolutely love the C# Language and am so happy that I decided to learn and embrace C# when .NET came on the scene and I transitioned from VB6. It is not to say VB is bad or inferior to C#, but that I really enjoy the tidiness and expressiveness of C#. It just speaks to me 🙂

With Visual Studio 2010 and the .NET Framework 4.0 we get a new version of the C# Language, C# 4.0.


C# 4.0 Optional Parameters and Named Parameters

A few of the interesting features in C# 4.0 are Optional Parameters, Default Values, and Named Parameters. The idea is that the arguments on a method may have “Default Values” and therefore it is unnecessary to supply those arguments in a method call if you are willing to accept those default values. This helps in those cases when we overload methods several times to help alleviate the caller from having to provide all values in a method. With Optional Parameters and Default Values you can now set Default Values to arguments on a method:


static void Write(string name, string address, string city = “Sarasota”) { … }


In the method above we have assigned the city parameter a default value of “Sarasota”, which means it can now be used optionally by the caller of the method:


Write(”David Hayden”, “1234 Broad Street”); city not specified, accept default.


or I can override the value by passing in the city as usual:


Write(“David Hayden“,“1234 Broad Street“,“Tampa“); // Overriding Default


Sometimes you may have multiple optional parameters:


static void Write(string name, string address, string city = “Sarasota”, string state=“Florida“) { … }


and the question becomes how do you specity a value for the state without specifying a value for the city? Named parameters, of course! We can now do the following:


Write(“David Hayden“,“1234 Broad Street“, state: “Hawaii“); 


Using the new “[parameter]:“ syntax is mind blowing and quite neat, no? 🙂


The only real convention here with optional parameters is that optional parameters must come at the end of the list of method arguments. Hence, you specify all your required arguments in the method first and then list the optional arguments last just like we did above.

The intellisense is pretty cool in Visual Studio 2010 for Optional Parameters, Default Values, and Named Parameters. Notice below how Visual Studio 2010 shows you the default values as well as pops-up the named parameter choices available for you to choose. Also notice in the screenshot that I chose to also use the named parameter syntax on the required zip parameter for kicks. That offers a bit of self-documenting code so you know what the values represent in the method call.


C# 4.0 Optional Parameters


Cool Stuff!!



Download Visual Studio 2010 Beta 1 and try out the C# 4.0 language features yourself.


David Hayden


Informazioni su pythonyan

.Net Solution Developer at Be Smart
Questa voce è stata pubblicata in C# 4.0. Contrassegna il permalink.


Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...