Click here to Skip to main content
15,885,934 members
Home / Discussions / C#
   

C#

 
Questionnecessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
BillWoodruff16-Jan-22 2:39
professionalBillWoodruff16-Jan-22 2: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 3:21
harold aptroot16-Jan-22 3:21 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
BillWoodruff16-Jan-22 5:14
professionalBillWoodruff16-Jan-22 5: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 6:56
harold aptroot16-Jan-22 6:56 
GeneralRe: necessity to use .Any() to check if an IEnumerable<T> result has no items without iterating over it ? Pin
BillWoodruff16-Jan-22 18:22
professionalBillWoodruff16-Jan-22 18: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 22:45
mveRichard Deeming16-Jan-22 22:45 
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 
BillWoodruff wrote:
I find this a specious comparison, in the context of this thread
Not sure I follow you here. You're writing code which can operate on an instance of any class that implements IEnumerable<T>. List<T> is such a class.

Your code wants to get the number of items in the source sequence, but only if that operation doesn't involve iterating over the entire collection. If the method was passed a List<T>, you can do it. If it was passed the result of an iterator method (for example), it can't.

It's all about examining the run-time type of the variable to access properties that aren't available on the compile-time type. From a purist perspective, that's not a good approach. But from a practical perspective, there are times when it's useful. Smile | :)

BillWoodruff wrote:
The source for IEnumerable.Coubt() uses the same short-circuiting behavior.
The difference is, if the value passed in doesn't match one of the specific interfaces, Enumerable.Count(IEnumerable<T>) will proceed to enumerate the entire sequence, whereas TryGetNonEnumeratedCount will give up.

For example, given:
C#
public static IEnumerable<int> Answers()
{
    while (true)
    {
        yield return 42;
    }
}
using:
C#
if (Answers().TryGetNonEnumeratedCount(out int count))
{
    Console.WriteLine(count);
}
else
{
    Console.WriteLine("Nope");
}
will output "Nope", whereas:
C#
int count = Answers().Count();
Console.WriteLine(count);
will never return.



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

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.