This style rule concerns the use of expression bodies versus block bodies for properties.
Options
Set the value of the associated option for this rule to specify whether expression bodies or block bodies for properties are preferred, and if expression bodies are preferred, whether they're preferred only for single-line expressions.
For more information about configuring options, see Option format.
csharp_style_expression_bodied_properties
Property
Value
Description
Option name
csharp_style_expression_bodied_properties
Option values
true
Prefer expression bodies for properties
when_on_single_line
Prefer expression bodies for properties when they will be a single line
false
Prefer block bodies for properties
Default option value
true
C#
// csharp_style_expression_bodied_properties = truepublicint Age => _age;
// csharp_style_expression_bodied_properties = falsepublicint Age { get { return _age; }}
This rule versus IDE0027
This rule, IDE0025, and IDE0027 (Use expression body for accessors) are very similar. IDE0025 concerns the property as a whole, whereas IDE0027 specifically concerns the accessor parts of the property.
For a read-only property that simply returns a value without doing any computation, if IDE0025 is set to csharp_style_expression_bodied_properties = false but IDE0027 is set to csharp_style_expression_bodied_accessors = true, you end up with a property that looks like this:
C#
publicint TemperatureF
{
get => _temp;
}
But if you set IDE0025 to csharp_style_expression_bodied_properties = true, the property is simplified even further (even if you set IDE0027 to csharp_style_expression_bodied_accessors = false):
C#
publicint TemperatureF => _temp;
For a read-write property, the difference becomes a little more apparent, because the property can't be written in an expression-bodied manner (because it consists of more than one line). So even if IDE0025 is set to csharp_style_expression_bodied_properties = true, you still end up with curly braces, that is, a block body.
The following examples show how a property looks with various combinations of the two options.
Izvor za ovaj sadržaj možete pronaći na GitHubu, gdje možete stvarati i pregledavati probleme i zahtjeve za povlačenjem. Dodatne informacije potražite u našem vodiču za suradnike.
Povratne informacije o proizvodu .NET
.NET je projekt otvorenog koda. Odaberite vezu za slanje povratnih informacija:
Pridružite se seriji susreta kako biste s kolegama programerima i stručnjacima izgradili skalabilna rješenja umjetne inteligencije temeljena na stvarnim slučajevima upotrebe.