Редактиране

Споделяне чрез


Use language keywords instead of framework type names for type references (IDE0049)

Property Value
Rule ID IDE0049
Title Use language keywords instead of framework type names for type references
Category Style
Subcategory Language rules (language keywords instead of framework type names)
Applicable languages C# and Visual Basic
Options dotnet_style_predefined_type_for_locals_parameters_members
dotnet_style_predefined_type_for_member_access

Overview

This rule concerns the use of language keywords, where they exist, instead of framework type names.

Note

Even if you enable code style rules on build, this rule is not enabled. It only surfaces in the Visual Studio editor.

Options

Use the associated options for this rule to apply it to:

An option value of true means prefer the language keyword (for example, int or Integer) instead of the type name (for example, Int32) for types that have a keyword to represent them. A value of false means prefer the type name instead of the language keyword.

For information about configuring options, see Option format.

dotnet_style_predefined_type_for_locals_parameters_members

Property Value Description
Option name dotnet_style_predefined_type_for_locals_parameters_members
Option values true Prefer the language keyword for local variables, method parameters, and class members
false Prefer the type name for local variables, method parameters, and class members
Default option value true
// dotnet_style_predefined_type_for_locals_parameters_members = true
private int _member;

// dotnet_style_predefined_type_for_locals_parameters_members = false
private Int32 _member;
' dotnet_style_predefined_type_for_locals_parameters_members = true
Private _member As Integer

' dotnet_style_predefined_type_for_locals_parameters_members = false
Private _member As Int32

dotnet_style_predefined_type_for_member_access

Property Value Description
Option name dotnet_style_predefined_type_for_member_access
Option values true Prefer the language keyword for member access expressions
false Prefer the type name for member access expressions
Default option value true
// dotnet_style_predefined_type_for_member_access = true
var local = int.MaxValue;

// dotnet_style_predefined_type_for_member_access = false
var local = Int32.MaxValue;
' dotnet_style_predefined_type_for_member_access = true
Dim local = Integer.MaxValue

' dotnet_style_predefined_type_for_member_access = false
Dim local = Int32.MaxValue

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.

#pragma warning disable IDE0049
// The code that's violating the rule is on this line.
#pragma warning restore IDE0049

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.IDE0049.severity = none

To disable all of the code-style rules, set the severity for the category Style to none in the configuration file.

[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none

For more information, see How to suppress code analysis warnings.

See also