-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add documentation for CA2263 (#39588)
- Loading branch information
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters