C# 4.0 New Features – Named Parameters
Aug18Written by:
2009/08/18 09:26 AM
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.
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.
blog comments powered by