Jun
6
Written by:
mark
6/6/2010 8:43 AM
I'm very excited about some of the features of .NET 4. The folks at Microsoft have added parallelization enhancements (for those who want to take advantage of multi-core environments), integrated the MEF (which opens up the world of plug-in architectures!), dynamic language features (simplifying the life of those unfortunate enough to have to deal with Interop
)- but sometimes it's the simple stuff that induces developer euphoria. Named and optional parameters are one (er- two?) of them.
For example, I use an implementation of RelayCommand very similar to that of Josh Smith. For those of you not in the know- ugh
- RelayCommand is a cornerstone of MVVM that allows us to define our implementations of ICommand in delegates. This means we don't have to create classes just to encapsulate a stinkin' command. Anyway, so RelayCommand has a constructor that expects two delegate parameters: one that's invoked to execute the command and one that's invoked to determine if the command is allowed to execute. I always forget the order that they're declared! Named parameters to save the day!
Now, instead of having to remember- or (gasp) stop and read the intellisense before typing- I can just do this:
new RelayCommand(canExecute:canAdd(), execute: param => add());
Not only does this help forgetful shlubs like myself, it's clearer what is going on.
Secondly, in the days before 4.0 (ah! I shall call it the b4.0 era) RelayCommand had to overload his constructor to make canExecute optional. Optional parameters to save the day!
Now she just needs one constructor that looks like this:
public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
Sometimes it's the little things that give us pleasure!
More often though, it's the big things. But this time it's little. Yeah.