a blog about Development and Tech stuff by Kristian S. Ravndal RSS 2.0
# Wednesday, April 15, 2009

Finally C# gets optional/default parameters!

One of the things I missed most, when moving from PHP to C#, was the way PHP let you pass along default parameters for any function. Now it look like C# 4.0 gets the same feature. Now I often find myself writing lots of overloaded methods, just because I want it to be easier to use the methods later on in the project

So this is how it is done today:
Illustation image

However, with the new feature in C#4.0, you can now put default values directly into the method:
Illustation image

That little line of code, covers all of the methods I wrote with C#3.5. The only thing to note, is that when you use similar input types for the parameters, it always used the first default values it finds. In the next example, using “foo(“s string”, false,10)” will set b = false, since it’s the first of 2 boolean default values.
Illustation image

Wednesday, April 15, 2009 8:34:51 PM (GMT Daylight Time, UTC+01:00)  #    Comments [1] - Trackback
C# | Tips&Tricks | Visual Studio
Monday, April 20, 2009 8:36:03 PM (GMT Daylight Time, UTC+01:00)
God, C# is good. Makes Java look like an outdated dinosaur by its side.
Comments are closed.
Navigation
Archive
<September 2010>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789
Other blogs
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Kristian Sølve Ravndal
Sign In
Statistics
Total Posts: 5
This Year: 0
This Month: 0
This Week: 0
Comments: 3
Themes
All Content © 2010, Kristian Sølve Ravndal
DasBlog theme 'Business' created by Christoph De Baene (delarou)