Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for CA2263 #39588

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions docs/fundamentals/code-analysis/quality-rules/ca2263.md
Original file line number Diff line number Diff line change
@@ -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 <xref:System.Type?displayProperty=fullName> 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 <xref:System.Type?displayProperty=fullName> 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<bool>();
```

```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).
1 change: 1 addition & 0 deletions docs/fundamentals/code-analysis/quality-rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<TResult>`](ca2261.md) | The `ConfigureAwaitOptions.SuppressThrowing` option isn't supported by the generic `Task<TResult>`, 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 <xref:System.Type?displayProperty=fullName> 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. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TResult>`](ca2261.md) | The `ConfigureAwaitOptions.SuppressThrowing` option isn't supported by the generic `Task<TResult>`, 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 <xref:System.Type?displayProperty=fullName> argument when the type is known, because they promote cleaner and more type-safe code with improved compile-time checks. |
2 changes: 2 additions & 0 deletions docs/navigate/tools-diagnostics/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading