Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This article describes performance insights for Enum.HasFlag.
Cause
Enum.HasFlag is resulting in slow performance.
Performance insight description
Using Enum.HasFlag in a performance sensitive context can lead to unnecessary memory allocations, such as expensive boxings and type checks.
.NET has optimized this method in .NET 5.0+. If you are building against these frameworks, then using Enum.HasFlag is typically fine. However, be aware that there are caveats that you should investigate. If performance issues remain, use bitwise operations instead.
How to investigate a warning
In the CPU Usage tool, click View source code to go to the call tree and source line highlighting view, which shows where the most amount of time is spent in Enum.HasFlag. From this point, trace back to where the method is used and check whether a bitwise operation should be used instead. You can also use the context menu to navigate from the Enum.HasFlag row in the call tree to the caller callee view to see every source location where Enum.HasFlag was found.