The Brave Programmer - Blogging and coding
Not for the faint hearted
 

Blog Posts From The Brave Programmer

Minimize

C# 4.0 New Features – Named Parameters

Aug 18

Written by:
2009/08/18 09:26 AM  RssIcon

C# csharp 4.0 With every release of a new language or a new version, there is always a slew of new features that accompany it. Otherwise what would be the point? Visual Studio 2010, .NET 4.x and C# 4.0 are no different. There is a list of new features that are bound to bring a smile to your face.

Sometimes new features are added to a language which fundamentally changes how we do things. e.g. Linq and Linq to SQL, which changed the way we access data. However, there are other times when features are added to the language that are “eye candy”, “syntax sugar” or “nice to have’s”.

This is always a good thing. Anything that makes life easier as a developer, or anything that makes coding faster or better to read is a good thing.

It is with this thought that I believe that Named Parameters is a good thing. It certainly should not be used in every case, but there are cases where it could be used and will make coding and debugging so much easier.

Named Parameters – an example

Named arguments allows us to specify the arguments by name instead of by position.  Code using the named parameters are often more readable than code relying on argument position. Named parameter is not new, in fact it is in C++ and has been in the  .net runtime.

Consider the following code:

PanelSwap( true, false,  true);
public void PanelSwap(bool panel1, bool panel2, bool panel3)
{
BluePanel.Visible = panel1;
RedPanel.Visible = panel2;
GreenPanel.Visible = panel3;
}

 

As you can see there is no way of knowing what the parameter stands for when calling the method. Because there are a few ways that the parameter can be mixed up, it is easy to call the method with the incorrect parameter values. Is it true or false?

Now if you could name each parameter it would be so easy. In the new C# 4.0, we can do this by supplying the name followed by a colon. Consider the following rewritten with named parameters.

PanelSwap(Blue: true, Red:false, Green:true);
public void PanelSwap( bool Blue=true, bool Red=true, bool Green=true)
{
BluePanel.Visible = Blue;
RedPanel.Visible = Red;
GreenPanel.Visible = Green;
}

Now we know exactly what each parameter stands for. The beauty about named parameters is that we can now supply them in any order we want because they will be resolved by name not position. So we can actually call the method like so:

PanelSwap(Green: true, Blue: false, Red: true); 

The other nice thing about this new feature is that our new named parameters are now listed in intellisense of Visual Studio 2010.

NamedParametersIntellisense

Not all your parameters have to be names. In fact you can have any number of parameters named,whether it is all of them or only a portion of your parameters. Consider the following code:

PanelSwap("This is passed!", Green: true, Blue: false,Red: true);
public void PanelSwap( string MyParameter, bool Blue=true, 
bool
Red=true, bool Green=true) { BluePanel.Visible = Blue; RedPanel.Visible = Red; GreenPanel.Visible = Green; label1.Text = MyParameter; }

One caveat though, is that named parameters have to be at the end of the parameter list.

Named parameters are also used in conjunction with another new C# 4.0 features, namely Optional and Default Parameters. As you can see the named parameters have a default value assigned to them. But that's for another post.

Related Reading:

Dynamic Types in C# 4.0 – Visual Studio 2010

Testing Visual Studio 2010

Introduction to Web Apps with C#3.0 and Visual studio 2008.

Have you used C# 4.0 yet in the Visual Studio 2010 beta? What do you think? Let us know in the comments below.

New here, or perhaps you've been here a few times? Like this post? Why not subscribe to this blog and get the most up to date posts as soon as they are published.



Tags:
Categories:
blog comments powered by Disqus
 
Blog Updates Via E-mail
  Blog Updates Via E-mail
Minimize

Do you want to receive blog updates via e-mail. Then just click on the link below. You will be redirected to Google's feed burner, where you can fill out a form. Supplying your e-mail address.

The subscription is managed entirely by Google's Feedburner. We cannot and do not collect your email address.

Subscribe to The Brave Programmer by Email

Print  
 

 

Latest Comments
  Latest Comments
Minimize
Powered by Disqus

Sign up with Disqus to enjoy a  surprise box of features

Print  
 
Blog Roll
  Blog Roll
Minimize
Print  
 
Categories
  Categories
Minimize
Print  
 
<h1>Search Blogs From The Brave Programmer</h1>
 

Search Blogs From The Brave Programmer

Minimize
Print  
 
Archive
  Archive
Minimize
Archive
<April 2024>
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
Monthly
Go
Print  
 
<h1>News Feeds (RSS)</h1>
 

News Feeds (RSS)

Minimize
Print  
 

Follow robertbravery on Twitter

Blog Engage Blog Forum and Blogging Community, Free Blog Submissions and Blog Traffic, Blog Directory, Article Submissions, Blog Traffic

View Robert Bravery's profile on LinkedIn

Mybyte

 

Robert - Find me on Bloggers.com

Tags
  Tags
Minimize
Print  
 
Contact Us Now
  Contact Us Now
Minimize
 

Email  us now or call us on 082-413-1420,  to host your website.

We design and develop websites. We develop websites that make a difference. We do Dotnetnuke Module development.

Web Masters Around The World
Power By Ringsurf
Print