diff --git a/docs/fundamentals/code-analysis/quality-rules/ca2263.md b/docs/fundamentals/code-analysis/quality-rules/ca2263.md new file mode 100644 index 0000000000000..572da2d56c8f1 --- /dev/null +++ b/docs/fundamentals/code-analysis/quality-rules/ca2263.md @@ -0,0 +1,82 @@ +--- +title: "CA2263: Prefer generic overload when type is known (code analysis)" +description: "Learn about code analyzer rule CA2263 - Prefer generic overload when type is known" +ms.date: 02/18/2024 +ms.topic: reference +f1_keywords: + - CA2263 + - PreferGenericOverloadsAnalyzer +helpviewer_keywords: + - CA2263 +author: mpidash +dev_langs: + - CSharp + - VB +--- + +# CA2263: Prefer generic overload when type is known + +| | Value | +| ----------------------------------- |--------------------------------------------| +| **Rule ID** | CA2263 | +| **Title** | Prefer generic overload when type is known | +| **Category** | [Usage](usage-warnings.md) | +| **Fix is breaking or non-breaking** | Non-breaking | +| **Enabled by default in .NET 9** | As suggestion | + +## Cause + +A method overload that accepts a argument is called when the type is known at compile time and a suitable generic overload is available. + +## Rule description + +Generic overloads are preferable to overloads that accept an argument of type when the type is known at compile time (using the [typeof operator](../../../csharp/language-reference/operators/type-testing-and-cast.md#typeof-operator) in C# or the [GetType operator](../../../visual-basic/language-reference/operators/gettype-operator.md) in Visual Basic). Generic overloads promote cleaner and more type-safe code with improved compile-time checks. + +## How to fix violations + +To fix a violation of this rule, use the suitable generic overload. + +## Example + +The following code snippet shows a violation of CA2263: + +```csharp +int size = Marshal.SizeOf(typeof(bool)); +``` + +```vb +Dim size As Integer = Marshal.SizeOf(GetType(Boolean)) +``` + +The following code snippet fixes the violation: + +```csharp +int size = Marshal.SizeOf(); +``` + +```vb +Dim size As Integer = Marshal.SizeOf(Of Boolean)() +``` + +## When to suppress warnings + +It is safe to suppress a warning from this rule; however, we recommend that you use a generic overload if possible. + +## Suppress a warning + +If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. + +```csharp +#pragma warning disable CA2263 +// The code that's violating the rule is on this line. +#pragma warning restore CA2263 +``` + +To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). + +```ini +[*.{cs,vb}] +dotnet_diagnostic.CA2263.severity = none +``` + +For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). diff --git a/docs/fundamentals/code-analysis/quality-rules/index.md b/docs/fundamentals/code-analysis/quality-rules/index.md index ad4a96c9c72f3..9b89a485353bd 100644 --- a/docs/fundamentals/code-analysis/quality-rules/index.md +++ b/docs/fundamentals/code-analysis/quality-rules/index.md @@ -233,6 +233,7 @@ The following table lists code quality analysis rules. > | [CA2260: Implement generic math interfaces correctly](ca2260.md) | Generic math interfaces require the derived type itself to be used for the self-recurring type parameter. | > | [CA2261: Do not use `ConfigureAwaitOptions.SuppressThrowing` with `Task`](ca2261.md) | The `ConfigureAwaitOptions.SuppressThrowing` option isn't supported by the generic `Task`, since that might lead to returning an invalid `TResult`. | > | [CA2262: Set `MaxResponseHeadersLength` properly](ca2262.md) | Make sure the `MaxResponseHeadersLength` value is provided correctly. This value is measured in kilobytes. | +> | [CA2263: Prefer generic overload when type is known](ca2263.md) | Using a generic overload is preferable to passing a argument when the type is known, because they promote cleaner and more type-safe code with improved compile-time checks. | > | [CA2300: Do not use insecure deserializer BinaryFormatter](ca2300.md) | Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. | > | [CA2301: Do not call BinaryFormatter.Deserialize without first setting BinaryFormatter.Binder](ca2301.md) | Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. | > | [CA2302: Ensure BinaryFormatter.Binder is set before calling BinaryFormatter.Deserialize](ca2302.md) | Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. | diff --git a/docs/fundamentals/code-analysis/quality-rules/usage-warnings.md b/docs/fundamentals/code-analysis/quality-rules/usage-warnings.md index 595ce9ac2b3a5..39ed3a622d7a3 100644 --- a/docs/fundamentals/code-analysis/quality-rules/usage-warnings.md +++ b/docs/fundamentals/code-analysis/quality-rules/usage-warnings.md @@ -62,3 +62,4 @@ Usage rules support proper usage of .NET. | [CA2260: Implement generic math interfaces correctly](ca2260.md) | Generic math interfaces require the derived type itself to be used for the self-recurring type parameter. | | [CA2261: Do not use `ConfigureAwaitOptions.SuppressThrowing` with `Task`](ca2261.md) | The `ConfigureAwaitOptions.SuppressThrowing` option isn't supported by the generic `Task`, since that might lead to returning an invalid `TResult`. | | [CA2262: Set `MaxResponseHeadersLength` properly](ca2262.md) | Make sure the `MaxResponseHeadersLength` value is provided correctly. This value is measured in kilobytes. | +| [CA2263: Prefer generic overload when type is known](ca2263.md) | Using a generic overload is preferable to passing a argument when the type is known, because they promote cleaner and more type-safe code with improved compile-time checks. | diff --git a/docs/navigate/tools-diagnostics/toc.yml b/docs/navigate/tools-diagnostics/toc.yml index f73f3cdd93341..0329c04810536 100644 --- a/docs/navigate/tools-diagnostics/toc.yml +++ b/docs/navigate/tools-diagnostics/toc.yml @@ -1377,6 +1377,8 @@ items: href: ../../fundamentals/code-analysis/quality-rules/ca2261.md - name: CA2262 href: ../../fundamentals/code-analysis/quality-rules/ca2262.md + - name: CA2263 + href: ../../fundamentals/code-analysis/quality-rules/ca2263.md - name: Code style rules items: - name: Overview