From 348646a39fdc827ce1b7eaca443372f82f195bed Mon Sep 17 00:00:00 2001 From: Wei Zheng <13881045+wzchua@users.noreply.github.com> Date: Fri, 29 Oct 2021 23:46:41 +0800 Subject: [PATCH] Add CA1850 documentation (#26634) --- .../code-analysis/quality-rules/ca1850.md | 89 +++++++++++++++++++ .../code-analysis/quality-rules/index.md | 1 + .../quality-rules/performance-warnings.md | 1 + docs/fundamentals/toc.yml | 2 + 4 files changed, 93 insertions(+) create mode 100644 docs/fundamentals/code-analysis/quality-rules/ca1850.md diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1850.md b/docs/fundamentals/code-analysis/quality-rules/ca1850.md new file mode 100644 index 0000000000000..ace7d917ac2dd --- /dev/null +++ b/docs/fundamentals/code-analysis/quality-rules/ca1850.md @@ -0,0 +1,89 @@ +--- +title: "CA1850: Prefer static `HashData` method over `ComputeHash`" +description: "It is more efficient to use the static `HashData` method over creating and managing a HashAlgorithm instance to call `ComputeHash`" +ms.date: 10/23/2021 +ms.topic: reference +f1_keywords: + - "CA1850" +helpviewer_keywords: + - "CA1850" +author: wzchua +dev_langs: +- CSharp +- VB +--- + +# CA1850: Prefer static `HashData` method over `ComputeHash` + +| | Value | +|-|-| +| **Rule ID** |CA1850| +| **Category** |[Performance](performance-warnings.md)| +| **Fix is breaking or non-breaking** |Non-breaking| + +## Cause + +An instance of a type that derives from is created to call its `ComputeHash` method, and that type has a static `HashData` method. + +## Rule description + +Static `HashData` methods were introduced in .NET 5 on the following types: + +- +- +- +- +- + +These methods help simplify code in cases where you just want to hash some data. + +It's more efficient to use theses static `HashData` methods than to create and manage a `HashAlgorithm` instance to call `ComputeHash`. + +## How to fix violations + +In general, you can fix the rule by changing your code to call `HashData` and remove use of the `HashAlgorithm` instance. + +```csharp +public bool CheckHash(byte[] buffer) +{ + using (var sha256 = SHA256.Create()) + { + byte[] digest = sha256.ComputeHash(buffer); + return DoesHashExist(digest); + } +} +``` + +```vb +Public Function CheckHash(buffer As Byte()) As Boolean + Using sha256 As SHA256 = SHA256.Create() + Dim digest As Byte() = sha256.ComputeHash(buffer) + Return DoesHashExist(digest) + End Using +End Function +``` + +The previous code can be changed to call the static method directly. + +```csharp +public bool CheckHash(byte[] buffer) +{ + byte[] digest = SHA256.HashData(buffer); + return DoesHashExist(digest); +} +``` + +```vb +Public Function CheckHash(buffer As Byte()) As Boolean + Dim digest As Byte() = SHA256.HashData(buffer) + Return DoesHashExist(digest) +End Function +``` + +## When to suppress warnings + +It is safe to suppress a warning from this rule. + +## See also + +- [Performance rules](performance-warnings.md) diff --git a/docs/fundamentals/code-analysis/quality-rules/index.md b/docs/fundamentals/code-analysis/quality-rules/index.md index 43626277b5ef9..25b12fffcb166 100644 --- a/docs/fundamentals/code-analysis/quality-rules/index.md +++ b/docs/fundamentals/code-analysis/quality-rules/index.md @@ -136,6 +136,7 @@ The following table lists code quality analysis rules. > | [CA1846: Prefer `AsSpan` over `Substring`](ca1846.md) | `AsSpan` is more efficient than `Substring`. `Substring` performs an O(n) string copy, while `AsSpan` does not and has a constant cost. `AsSpan` also does not perform any heap allocations. | > | [CA1847: Use char literal for a single character lookup](ca1847.md) | Use `string.Contains(char)` instead of `string.Contains(string)` when searching for a single character. | > | [CA1849: Call async methods when in an async method](ca1849.md) | In a method which is already asynchronous, calls to other methods should be to their async versions, where they exist. | +> | [CA1850: Prefer static `HashData` method over `ComputeHash`](ca1850.md) | It's more efficient to use the static `HashData` method over creating and managing a `HashAlgorithm` instance to call `ComputeHash`. | > | [CA2000: Dispose objects before losing scope](ca2000.md) | Because an exceptional event might occur that will prevent the finalizer of an object from running, the object should be explicitly disposed before all references to it are out of scope. | > |[CA2002: Do not lock on objects with weak identity](ca2002.md) |An object is said to have a weak identity when it can be directly accessed across application domain boundaries. A thread that tries to acquire a lock on an object that has a weak identity can be blocked by a second thread in a different application domain that has a lock on the same object. | > | [CA2007: Do not directly await a Task](ca2007.md) | An asynchronous method [awaits](../../../csharp/language-reference/operators/await.md) a directly. When an asynchronous method awaits a directly, continuation occurs in the same thread that created the task. This behavior can be costly in terms of performance and can result in a deadlock on the UI thread. Consider calling to signal your intention for continuation. | diff --git a/docs/fundamentals/code-analysis/quality-rules/performance-warnings.md b/docs/fundamentals/code-analysis/quality-rules/performance-warnings.md index 9af68c94bffd1..3b4ca0217fbfe 100644 --- a/docs/fundamentals/code-analysis/quality-rules/performance-warnings.md +++ b/docs/fundamentals/code-analysis/quality-rules/performance-warnings.md @@ -55,3 +55,4 @@ Performance rules support high-performance libraries and applications. | [CA1846: Prefer `AsSpan` over `Substring`](ca1846.md) | `AsSpan` is more efficient than `Substring`. `Substring` performs an O(n) string copy, while `AsSpan` does not and has a constant cost. `AsSpan` also does not perform any heap allocations. | | [CA1847: Use char literal for a single character lookup](ca1847.md) | Use `string.Contains(char)` instead of `string.Contains(string)` when searching for a single character. | | [CA1849: Call async methods when in an async method](ca1849.md) | Use `string.Contains(char)` instead of `string.Contains(string)` when searching for a single character. | +| [CA1850: Prefer static `HashData` method over `ComputeHash`](ca1850.md) | It's more efficient to use the static `HashData` method over creating and managing a `HashAlgorithm` instance to call `ComputeHash`. | diff --git a/docs/fundamentals/toc.yml b/docs/fundamentals/toc.yml index 512a2f3dbcf0d..1039f17bcad59 100644 --- a/docs/fundamentals/toc.yml +++ b/docs/fundamentals/toc.yml @@ -981,6 +981,8 @@ items: href: code-analysis/quality-rules/ca1847.md - name: CA1849 href: code-analysis/quality-rules/ca1849.md + - name: CA1850 + href: code-analysis/quality-rules/ca1850.md - name: SingleFile rules items: - name: Overview