Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
GeneralRe: better way to Linqify this ? Pin
PIEBALDconsult9-Jan-16 16:05
mvePIEBALDconsult9-Jan-16 16:05 
GeneralRe: better way to Linqify this ? Pin
BillWoodruff11-Jan-16 8:10
professionalBillWoodruff11-Jan-16 8:10 
GeneralRe: better way to Linqify this ? Pin
PIEBALDconsult11-Jan-16 14:33
mvePIEBALDconsult11-Jan-16 14:33 
GeneralRe: better way to Linqify this ? Pin
Richard Deeming12-Jan-16 1:31
mveRichard Deeming12-Jan-16 1:31 
GeneralRe: better way to Linqify this ? Pin
Jörgen Andersson9-Jan-16 21:32
professionalJörgen Andersson9-Jan-16 21:32 
GeneralRe: better way to Linqify this ? Pin
Richard Deeming11-Jan-16 3:31
mveRichard Deeming11-Jan-16 3:31 
GeneralRe: better way to Linqify this ? Pin
Jörgen Andersson11-Jan-16 6:31
professionalJörgen Andersson11-Jan-16 6:31 
GeneralRe: better way to Linqify this ? Pin
Jörgen Andersson10-Jan-16 11:37
professionalJörgen Andersson10-Jan-16 11:37 
I realized from Piebalds response below that I owe you a proper answer.

If you take a look at the IEnumerable(T) Interface[^] it specifies only one Method, GetEnumerator.
All other methods are extensions that depend on that one single method.

So if we look at the IEnumerator(T) Interface[^] you'll notice that it has only three methods.
Dispose is of no interest here. MoveNext means that it's a forward only enumerator.
But note that Reset does not need to be implemented. which means you cannot count on getting restarted. So you can only count on enumerating once.

This is why I wrote that "it would consume the IEnumerable while counting".


So why did your code work?
I guess your Source parameter also implemented the ICollection(T) Interface[^] which specifies the Count property. (A List<T> for example)
And if you have a Class property/method with the same signature as an Extension method, the Class property/method will always take priority.
The compiler never complained since it could see the Extension method Count()

Therefore I should have written "it might consume the IEnumerable while counting" or maybe rather "might enumerate".

Luckily the extension method Count and the Class property Count, does the same thing. Smile | :)

BillWoodruff wrote:
Perhaps it is the case that transforming the IEnumerable to a List<T;> is a good thing to do, if it needs to be evaluated more than once.
In general I'd say yes, but it probably depends on whether the cost of instantiating objects or saving memory is of the highest importance.
Wrong is evil and must be defeated. - Jeff Ello

GeneralRe: better way to Linqify this ? Pin
PIEBALDconsult10-Jan-16 14:09
mvePIEBALDconsult10-Jan-16 14:09 
GeneralRe: better way to Linqify this ? Pin
BillWoodruff11-Jan-16 8:01
professionalBillWoodruff11-Jan-16 8:01 
GeneralRe: better way to Linqify this ? Pin
PIEBALDconsult11-Jan-16 14:32
mvePIEBALDconsult11-Jan-16 14:32 
GeneralRe: better way to Linqify this ? Pin
BillWoodruff11-Jan-16 7:52
professionalBillWoodruff11-Jan-16 7:52 
GeneralRe: better way to Linqify this ? Pin
PIEBALDconsult10-Jan-16 9:56
mvePIEBALDconsult10-Jan-16 9:56 
GeneralRe: better way to Linqify this ? Pin
Jörgen Andersson10-Jan-16 10:55
professionalJörgen Andersson10-Jan-16 10:55 
GeneralRe: better way to Linqify this ? Pin
PIEBALDconsult9-Jan-16 16:05
mvePIEBALDconsult9-Jan-16 16:05 
AnswerRe: better way to Linqify this ? Pin
Luc Pattyn9-Jan-16 6:13
sitebuilderLuc Pattyn9-Jan-16 6:13 
GeneralRe: better way to Linqify this ? Pin
PIEBALDconsult9-Jan-16 16:45
mvePIEBALDconsult9-Jan-16 16:45 
GeneralRe: better way to Linqify this ? Pin
Luc Pattyn9-Jan-16 16:52
sitebuilderLuc Pattyn9-Jan-16 16:52 
GeneralRe: better way to Linqify this ? Pin
Nish Nishant13-Jan-16 10:24
sitebuilderNish Nishant13-Jan-16 10:24 
GeneralRe: better way to Linqify this ? Pin
Luc Pattyn13-Jan-16 12:17
sitebuilderLuc Pattyn13-Jan-16 12:17 
GeneralRe: better way to Linqify this ? Pin
Nish Nishant14-Jan-16 7:25
sitebuilderNish Nishant14-Jan-16 7:25 
AnswerRe: better way to Linqify this ? Pin
Richard Deeming11-Jan-16 3:29
mveRichard Deeming11-Jan-16 3:29 
GeneralRe: better way to Linqify this ? Pin
BillWoodruff11-Jan-16 8:20
professionalBillWoodruff11-Jan-16 8:20 
GeneralRe: better way to Linqify this ? Pin
Richard Deeming11-Jan-16 8:48
mveRichard Deeming11-Jan-16 8:48 
GeneralRe: better way to Linqify this ? Pin
BillWoodruff11-Jan-16 10:27
professionalBillWoodruff11-Jan-16 10:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.