Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This article describes two related rules, IDE0007 and IDE0008.
| Property | Value |
|---|---|
| Rule ID | IDE0007 |
| Title | Use var instead of explicit type |
| Category | Style |
| Subcategory | Language rules ('var' preferences) |
| Applicable languages | C# |
| Options | csharp_style_var_for_built_in_types |
csharp_style_var_when_type_is_apparent |
|
csharp_style_var_elsewhere |
| Property | Value |
|---|---|
| Rule ID | IDE0008 |
| Title | Use explicit type instead of var |
| Category | Style |
| Subcategory | Language rules ('var' preferences) |
| Applicable languages | C# |
| Options | csharp_style_var_for_built_in_types |
csharp_style_var_when_type_is_apparent |
|
csharp_style_var_elsewhere |
Overview
These two style rules define whether the var keyword or an explicit type should be used in a variable declaration. To enforce that var is used, set the severity of IDE0007 to warning or error. To enforce that the explicit type is used, set the severity of IDE0008 to warning or error.
Options
This rule's associated options define where this style preference should be applied:
- Built-in types (csharp_style_var_for_built_in_types)
- Places where the type is apparent (csharp_style_var_when_type_is_apparent)
- Elsewhere (csharp_style_var_elsewhere)
For more information about configuring options, see Option format.
csharp_style_var_for_built_in_types
| Property | Value | Description |
|---|---|---|
| Option name | csharp_style_var_for_built_in_types | |
| Option values | true |
Prefer var is used to declare variables with built-in system types such as int |
false |
Prefer explicit type over var to declare variables with built-in system types such as int |
|
| Default option value | false |
// csharp_style_var_for_built_in_types = true
var x = 5;
// csharp_style_var_for_built_in_types = false
int x = 5;
csharp_style_var_when_type_is_apparent
| Property | Value | Description |
|---|---|---|
| Option name | csharp_style_var_when_type_is_apparent | |
| Option values | true |
Prefer var when the type is already mentioned on the right-hand side of a declaration expression |
false |
Prefer explicit type when the type is already mentioned on the right-hand side of a declaration expression | |
| Default option value | false |
// csharp_style_var_when_type_is_apparent = true
var obj = new Customer();
// csharp_style_var_when_type_is_apparent = false
Customer obj = new Customer();
csharp_style_var_elsewhere
| Property | Value | Description |
|---|---|---|
| Option name | csharp_style_var_elsewhere | |
| Option values | true |
Prefer var over explicit type in all cases, unless overridden by another code style rule |
false |
Prefer explicit type over var in all cases, unless overridden by another code style rule |
|
| Default option value | false |
// csharp_style_var_elsewhere = true
var f = this.Init();
// csharp_style_var_elsewhere = false
bool f = this.Init();
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 IDE0007 // Or IDE0008
// The code that's violating the rule is on this line.
#pragma warning restore IDE0007 // Or IDE0008
To disable the rule for a file, folder, or project, set its severity to none in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.IDE0007.severity = none
dotnet_diagnostic.IDE0008.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.