Click here to Skip to main content
15,844,387 members
Home / Discussions / C#
   

C#

 
AnswerRe: Exceptions Pin
Eddy Vluggen18-Jan-22 19:02
professionalEddy Vluggen18-Jan-22 19:02 
AnswerRe: Exceptions Pin
jschell23-Jan-22 8:32
jschell23-Jan-22 8:32 
Questionnecessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
BillWoodruff16-Jan-22 3:39
professionalBillWoodruff16-Jan-22 3:39 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
harold aptroot16-Jan-22 4:21
harold aptroot16-Jan-22 4:21 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
BillWoodruff16-Jan-22 6:14
professionalBillWoodruff16-Jan-22 6:14 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
harold aptroot16-Jan-22 7:56
harold aptroot16-Jan-22 7:56 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
BillWoodruff16-Jan-22 19:22
professionalBillWoodruff16-Jan-22 19:22 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
Richard Deeming16-Jan-22 23:45
mveRichard Deeming16-Jan-22 23:45 
BillWoodruff wrote:
If an IEnumerable "knew" its count ... is that a contradiction in terms ?
Not really. For example, a List<T> is also an IEnumerable<T>; the List<T> "knows" its count.

The TryGetNonEnumeratedCount method simply attempts to cast the IEnumerable<T> to a few "pre-counted" interfaces at run-time, and returns the Count property from the first one that succeeds. You can see the source code on GitHub:

runtime/Count.cs at 57bfe474518ab5b7cfe6bf7424a79ce3af9d6657 · dotnet/runtime · GitHub[^]

It tries ICollection<T>, IIListProvider<T>, and the non-generic ICollection. That should cover most cases, although it notably doesn't try the IReadOnlyCollection<T> introduced in .NET 4.5 - I can't find an official comment, but this SO thread[^] discusses that.

You can see the original proposal for this method on the runtime repository:
Non-enumerating Count Linq Method · Issue #27183 · dotnet/runtime · GitHub[^]



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
BillWoodruff18-Jan-22 2:19
professionalBillWoodruff18-Jan-22 2: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 2:41
mveRichard Deeming18-Jan-22 2:41 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
BillWoodruff18-Jan-22 4:08
professionalBillWoodruff18-Jan-22 4: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 7:17
mveRichard Deeming18-Jan-22 7:17 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
BillWoodruff19-Jan-22 14:58
professionalBillWoodruff19-Jan-22 14: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 22:43
mveRichard Deeming19-Jan-22 22:43 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
BillWoodruff20-Jan-22 5:26
professionalBillWoodruff20-Jan-22 5: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 23:48
harold aptroot16-Jan-22 23:48 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
BillWoodruff18-Jan-22 1:49
professionalBillWoodruff18-Jan-22 1:49 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
jschell23-Jan-22 8:40
jschell23-Jan-22 8:40 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
BillWoodruff25-Jan-22 4:58
professionalBillWoodruff25-Jan-22 4:58 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
jschell30-Jan-22 7:22
jschell30-Jan-22 7:22 
QuestionRemove a page from pdf document in C# Pin
Member 1447460712-Jan-22 21:09
Member 1447460712-Jan-22 21:09 
AnswerRe: Remove a page from pdf document in C# Pin
OriginalGriff12-Jan-22 21:22
mveOriginalGriff12-Jan-22 21:22 
GeneralRe: Remove a page from pdf document in C# Pin
Member 1447460713-Jan-22 6:19
Member 1447460713-Jan-22 6:19 
GeneralRe: Remove a page from pdf document in C# Pin
OriginalGriff13-Jan-22 6:39
mveOriginalGriff13-Jan-22 6:39 
QuestionRoots of polynomial , eigenvalue method Pin
Member 1549773912-Jan-22 7:08
Member 1549773912-Jan-22 7:08 

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.