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

C#

 
GeneralRe: better way to Linqify this ? Pin
Richard Deeming11-Jan-16 11:00
mveRichard Deeming11-Jan-16 11:00 
GeneralRe: better way to Linqify this ? Pin
Sascha Lefèvre11-Jan-16 23:00
professionalSascha Lefèvre11-Jan-16 23:00 
GeneralRe: better way to Linqify this ? Pin
BillWoodruff12-Jan-16 0:19
professionalBillWoodruff12-Jan-16 0:19 
AnswerRe: better way to Linqify this ? Pin
PIEBALDconsult9-Jan-16 6:09
mvePIEBALDconsult9-Jan-16 6:09 
GeneralRe: better way to Linqify this ? Pin
OriginalGriff9-Jan-16 6:17
mveOriginalGriff9-Jan-16 6:17 
GeneralRe: better way to Linqify this ? Pin
Jörgen Andersson9-Jan-16 12:53
professionalJörgen Andersson9-Jan-16 12:53 
GeneralRe: better way to Linqify this ? Pin
BillWoodruff9-Jan-16 13:52
professionalBillWoodruff9-Jan-16 13:52 
GeneralRe: better way to Linqify this ? Pin
PIEBALDconsult9-Jan-16 16:05
mvePIEBALDconsult9-Jan-16 16:05 
BillWoodruff wrote:
because a source IEnumerable can be "used" any number of times.


Not all of them; and you can't tell. Queue and Stack implement IEnumerable, but they can be consumed only once (fortunately they have Count properties).


BillWoodruff wrote:
whether multiple evaluations of the IEnumerable source are very expensive ... in terms of memory, time.


It may have to enumerate it fully; that takes time. Enumerating may also involve file or network access or similar (e.g. database access, reading from a socket) that uses time, IO, and memory.
And this particular result is not worth the effort in this case; so it's a waste.


Of course, it's possible that the Count method you use checks for certain types (e.g. Stack, Queue, Array, String) or interfaces (e.g. IList) and then uses the appropriate Count or Length methods rather than enumerating, but failing that, it must enumerate.

But the bottom line, in this case, is that there is no reason to check the Count anyway. And as Luc pointed out, if you're checking the Count you might as well check the chunksz before trying to divide by it. Which then leads to the question "what to do when the caller specifies a chunksz of zero?" -- and I suspect the "best" thing to do is to treat it as an "all the rest" value. But that's just my thought.


I suggest leaving the burden of checking such things to the caller. Document what the method does and let the buyer beware.

modified 9-Jan-16 23:34pm.

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 
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 

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.