Why do I love Extension Methods in System.Linq ?
.Net January 16th, 2009Because it helps me to write compact and clean code of course !
For example, I need to expose a property that gives the number of visible items in my ViewModel.
Old way:
1 2 3 4 5 6 7 8 9 10 11 12 13 | public int VisibleItemsCount { get { int i = 0; foreach (var item in this.Items) { if (item.IsVisible) i++; } return i; } } |
Linq way:
1 2 3 4 5 6 7 | public int VisibleItemsCount { get { return this.Items.Count(item => item.IsVisible); } } |
Which one do you prefer ?


January 16th, 2009 at 12:42 pm
Si je puis me permettre, le titre du post aurait du être:
“Why I love Extension Methods ?”
La méthode Count est une méthode d’extension: rien à voir avec LINQ
A+
January 16th, 2009 at 1:59 pm
J’ai mis à jour le titre
Effectivement, rien à avoir avec Linq en lui-même, mais la méthode se trouvant dans System.Linq, j’ai pris un raccourci !