Docs: Using List.Length instead of List.Any()

Created on 18 May 2020  Â·  10Comments  Â·  Source: dotnet/docs

Do you recommend using allTasks.Any() instead of allTasks.Length > 0? I think that documentation must provide the right path to write code in programming guide. Why not to use Length where it is possible? People learning this languages then face the job interviews and failed at this point.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Area - C# Guide Technology - Async Task doc-enhancement

All 10 comments

Do you mean Count, not Length? I think List doesn't have a Length property but have Count.

I'll try to run a benchmark test against both cases.

Count seem to be faster:

image

But let me ping @stephentoub to check if that's correct before I make a PR.

Dancing away: what if to move the Console.WriteLine calls inside the corresponding async methods? Then, code in general would be shorter. In particular, tasks.WhenAll becomes possible without existing while loop?

@pkulikov, I like that!
While moving the Console.WriteLine calls to the corresponding async method, The issue https://github.com/dotnet/docs/issues/16987 will get fixed too.

This is also a good opportunity to move the snippets and follow the new structure.

If a type exposes Length/Count, from a micro-optimization perspective it's preferable to use that instead of Any(); from a readability perspective, I expect developer's opinions reasonably differ about which is more clear. In this code sample, I highly doubt the difference in throughout will be meaningful, given the other costs involved. Even so, it's fine to change from my perspective.

I'm fine will all the changes @pkulikov and @Youssef1313 have suggested.

Thanks all.

@Youssef1313 my guess is that you wanted to submit a PR?

@pkulikov I haven't started on it yet, I'm okay if you want to take it. If not, inform me and I'll try to start soon.

@Youssef1313 no, I don't want to take it, please proceed.

In particular, tasks.WhenAll becomes possible without existing while loop?

While working on the issue, I found that WhenAll was already explained, then WhenAny was introduced as another option. So, I'll keep the code as-is for this part. and just move the snippets, make minor changes, and use .Count instead of .Any().

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tswett picture tswett  Â·  3Comments

stjepan picture stjepan  Â·  3Comments

gmatv picture gmatv  Â·  3Comments

sdmaclea picture sdmaclea  Â·  3Comments

ite-klass picture ite-klass  Â·  3Comments