אירוע
בניית אפליקציות וסוכנים של בינה מלאכותית
17 במרץ, 21 - 21 במרץ, 10
הצטרף לסידרה של פגישות כדי לבנות פתרונות מדרגיים של בינה מלאכותית בהתבסס על מקרי שימוש מהעולם האמיתי עם מפתחים ומומחים אחרים.
הירשם עכשיוהדפדפן הזה אינו נתמך עוד.
שדרג ל- Microsoft Edge כדי לנצל את התכונות, עדכוני האבטחה והתמיכה הטכנית העדכניים ביותר.
Property | Value |
---|---|
Rule ID | CA1828 |
Title | Do not use CountAsync/LongCountAsync when AnyAsync can be used |
Category | Performance |
Fix is breaking or non-breaking | Non-breaking |
Enabled by default in .NET 9 | As suggestion |
The CountAsync or LongCountAsync method was used where the AnyAsync method would be more efficient.
This rule flags the CountAsync and LongCountAsync LINQ method calls used to check if the collection has at least one element. These method calls require enumerating the entire collection to compute the count. The same check is faster with the AnyAsync method as it avoids enumerating the collection.
To fix a violation, replace the CountAsync or LongCountAsync method call with the AnyAsync method. For example, the following two code snippets show a violation of the rule and how to fix it:
using System.Linq;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions;
class C
{
public async Task<string> M1Async(IQueryable<string> list)
=> await list.CountAsync() != 0 ? "Not empty" : "Empty";
public async Task<string> M2Async(IQueryable<string> list)
=> await list.LongCountAsync() > 0 ? "Not empty" : "Empty";
}
using System.Linq;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions;
class C
{
public async Task<string> M1Async(IQueryable<string> list)
=> await list.AnyAsync() ? "Not empty" : "Empty";
public async Task<string> M2Async(IQueryable<string> list)
=> await list.AnyAsync() ? "Not empty" : "Empty";
}
עצה
A code fix is available for this rule in Visual Studio. To use it, position the cursor on the violation and press Ctrl+. (period). Choose Do not use CountAsync() or LongCountAsync() when AnyAsync() can be used from the list of options that's presented.
It's safe to suppress a violation of this rule if you're not concerned about the performance impact from unnecessary collection enumeration to compute the count.
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable CA1828
// The code that's violating the rule is on this line.
#pragma warning restore CA1828
To disable the rule for a file, folder, or project, set its severity to none
in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA1828.severity = none
For more information, see How to suppress code analysis warnings.
משוב של .NET
.NET הוא פרויקט קוד פתוח. בחר קישור כדי לספק משוב:
אירוע
בניית אפליקציות וסוכנים של בינה מלאכותית
17 במרץ, 21 - 21 במרץ, 10
הצטרף לסידרה של פגישות כדי לבנות פתרונות מדרגיים של בינה מלאכותית בהתבסס על מקרי שימוש מהעולם האמיתי עם מפתחים ומומחים אחרים.
הירשם עכשיו