forked from dotnet/docs
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
75 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,71 @@ | ||
--- | ||
title: "IDE0280: Use 'nameof'" | ||
description: "Learn about code analysis rule IDE0280: Use 'nameof'" | ||
ms.date: 07/26/2023 | ||
ms.topic: reference | ||
f1_keywords: | ||
- IDE0280 | ||
helpviewer_keywords: | ||
- IDE0280 | ||
dev_langs: | ||
- CSharp | ||
--- | ||
# Use 'nameof' (IDE0280) | ||
|
||
| Property | Value | | ||
|--------------------------|-----------------------------------------------| | ||
| **Rule ID** | IDE0280 | | ||
| **Title** | Use 'nameof' | | ||
| **Category** | Style | | ||
| **Subcategory** | Language rules (expression-level preferences) | | ||
| **Applicable languages** | C# | | ||
|
||
## Overview | ||
|
||
This rule flags the use of a literal parameter name instead of the [`nameof` expression](../../../csharp/language-reference/operators/nameof.md) in attributes such as <xref:System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute>, <xref:System.Diagnostics.CodeAnalysis.NotNullWhenAttribute>, and <xref:System.Runtime.CompilerServices.CallerArgumentExpressionAttribute> that take a parameter name. | ||
|
||
## Options | ||
|
||
This rule has no associated code-style options. | ||
|
||
## Example | ||
|
||
```csharp | ||
// Code with violations. | ||
class C | ||
{ | ||
void M([NotNullIfNotNull("input")] string? input) { } | ||
} | ||
|
||
// Fixed code. | ||
class C | ||
{ | ||
void M([NotNullIfNotNull(nameof(input))] string? input) { } | ||
} | ||
``` | ||
|
||
## Suppress a warning | ||
|
||
If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. | ||
|
||
```csharp | ||
#pragma warning disable IDE0280 | ||
// The code that's violating the rule is on this line. | ||
#pragma warning restore IDE0280 | ||
``` | ||
|
||
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.IDE0280.severity = none | ||
``` | ||
|
||
To disable all of the code-style rules, set the severity for the category `Style` to `none` in the [configuration file](../configuration-files.md). | ||
|
||
```ini | ||
[*.{cs,vb}] | ||
dotnet_analyzer_diagnostic.category-Style.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