Click here to Skip to main content
15,886,059 members
Home / Discussions / C#
   

C#

 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
BillWoodruff18-Jan-22 1:19
professionalBillWoodruff18-Jan-22 1:19 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
Richard Deeming18-Jan-22 1:41
mveRichard Deeming18-Jan-22 1:41 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
BillWoodruff18-Jan-22 3:08
professionalBillWoodruff18-Jan-22 3:08 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
Richard Deeming18-Jan-22 6:17
mveRichard Deeming18-Jan-22 6:17 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
BillWoodruff19-Jan-22 13:58
professionalBillWoodruff19-Jan-22 13:58 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
Richard Deeming19-Jan-22 21:43
mveRichard Deeming19-Jan-22 21:43 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
BillWoodruff20-Jan-22 4:26
professionalBillWoodruff20-Jan-22 4:26 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
harold aptroot16-Jan-22 22:48
harold aptroot16-Jan-22 22:48 
BillWoodruff wrote:
Clearly, some collections "know" their count, because it is a Property not a method.. But, a generic IEnumerable created at run-time ?
Right, so for most things, TryGetNonEnumeratedCount just returns false. It doesn't give the count in general, it gives the count in special cases such as when the IEnumerable<T> is actually a List<T> or something similar that has a known count, that's the point. BTW you can check the source.
BillWoodruff wrote:
fascinating; I try to imagine a case of a generic IEnumerable where Count() would be more efficient ... but, draw a blank
There's little chance of that, it's about the special cases again, you can see someone elses benchmarks here. They are cases in which Any and Count are both pretty fast (no significant iteration happens), but not equally fast and Count won. Those results are probably not valid anymore for .NET 6, in which Any has similar shortcuts as Count and TryGetNonEnumeratedCount have, but it made sense back in the days of .NET 4.8 and such, back then Count already had the shortcuts (TryGetNonEnumeratedCount didn't exist yet) but Any didn't have shortcuts so it wasted time creating an enumerator and calling MoveNext on it even for collections with a known count.

About 3 & 4, it depends. Depending on where those IEnumerables come from, that might perform a database query twice or call a function with a side-effect extra times (the IEnumerable could have different contents the next time, including changing its length). The safe thing to do is ToList-ing the IEnumerable at this point.. but that's the opposite of what you were trying to do. The predicate passed into a (for example) Where should be a pure function (then multiple evaluations are safe, just cost time) but C# has no mechanism to enforce that.

Sticking with Where for now, aside from literally changing the result, there is also potentially a lot of time wasted (depending on where the source IEnumerable comes from, how expensive the Where predicate is, and how often the predicate is true) by first finding the first element that passes the predicate (for Any), and then doing it again when the IEnumerable is used "for real". It's avoidable but a little messy. Duplicate work is easier to avoid in the case that Any returns false.

TBH I'm leaning more and more in favour of avoiding this whole mess, but IDK what kind of algorithm you're dealing with..
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
BillWoodruff18-Jan-22 0:49
professionalBillWoodruff18-Jan-22 0:49 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
jschell23-Jan-22 7:40
jschell23-Jan-22 7:40 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
BillWoodruff25-Jan-22 3:58
professionalBillWoodruff25-Jan-22 3:58 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
jschell30-Jan-22 6:22
jschell30-Jan-22 6:22 
QuestionRemove a page from pdf document in C# Pin
Member 1447460712-Jan-22 20:09
Member 1447460712-Jan-22 20:09 
AnswerRe: Remove a page from pdf document in C# Pin
OriginalGriff12-Jan-22 20:22
mveOriginalGriff12-Jan-22 20:22 
GeneralRe: Remove a page from pdf document in C# Pin
Member 1447460713-Jan-22 5:19
Member 1447460713-Jan-22 5:19 
GeneralRe: Remove a page from pdf document in C# Pin
OriginalGriff13-Jan-22 5:39
mveOriginalGriff13-Jan-22 5:39 
QuestionRoots of polynomial , eigenvalue method Pin
Member 1549773912-Jan-22 6:08
Member 1549773912-Jan-22 6:08 
AnswerRe: Roots of polynomial , eigenvalue method Pin
Victor Nijegorodov12-Jan-22 6:22
Victor Nijegorodov12-Jan-22 6:22 
GeneralRe: Roots of polynomial , eigenvalue method Pin
Member 1549773912-Jan-22 8:33
Member 1549773912-Jan-22 8:33 
AnswerRe: Roots of polynomial , eigenvalue method Pin
Richard Andrew x6412-Jan-22 7:10
professionalRichard Andrew x6412-Jan-22 7:10 
GeneralRe: Roots of polynomial , eigenvalue method Pin
Member 1549773912-Jan-22 8:34
Member 1549773912-Jan-22 8:34 
GeneralRe: Roots of polynomial , eigenvalue method Pin
Richard Andrew x6412-Jan-22 8:38
professionalRichard Andrew x6412-Jan-22 8:38 
AnswerRe: Roots of polynomial , eigenvalue method Pin
Kenneth Haugland15-Jan-22 1:53
mvaKenneth Haugland15-Jan-22 1:53 
QuestionSerial communication between LPC2132 microcontroller and serial interface PC through RS 232 and RJ45 Pin
Member 1549617511-Jan-22 2:06
Member 1549617511-Jan-22 2:06 
AnswerRe: Serial communication between LPC2132 microcontroller and serial interface PC through RS 232 and RJ45 Pin
OriginalGriff11-Jan-22 2:21
mveOriginalGriff11-Jan-22 2:21 

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.