Expression body definitions let you provide a member's implementation in a concise, readable form. You can use an expression body definition whenever the logic for any supported member, such as a method or property, consists of a single expression. An expression body definition has the following general syntax:
C#
member => expression;
where expression is a valid expression.
Expression body definitions can be used with the following type members:
An expression-bodied method consists of a single expression that returns a value whose type matches the method's return type, or, for methods that return void, that performs some operation. For example, types that override the ToString method typically include a single expression that returns the string representation of the current object.
The following example defines a Person class that overrides the ToString method with an expression body definition. It also defines a DisplayName method that displays a name to the console. The return keyword is not used in the ToString expression body definition.
You can use expression body definition to implement a read-only property. To do that, use the following syntax:
C#
PropertyType PropertyName => expression;
The following example defines a Location class whose read-only Name property is implemented as an expression body definition that returns the value of the private locationName field:
An expression body definition for a constructor typically consists of a single assignment expression or a method call that handles the constructor's arguments or initializes instance state.
The following example defines a Location class whose constructor has a single string parameter named name. The expression body definition assigns the argument to the Name property.
C#
publicclassLocation
{
privatestring locationName;
publicLocation(string name) => Name = name;
publicstring Name
{
get => locationName;
set => locationName = value;
}
}
Like with properties, indexer get and set accessors consist of expression body definitions if the get accessor consists of a single expression that returns a value or the set accessor performs a simple assignment.
The following example defines a class named Sports that includes an internal String array that contains the names of some sports. Both the indexer get and set accessors are implemented as expression body definitions.
C#
using System;
using System.Collections.Generic;
namespaceSportsExample;
publicclassSports
{
privatestring[] types = [ "Baseball", "Basketball", "Football",
"Hockey", "Soccer", "Tennis",
"Volleyball" ];
publicstringthis[int i]
{
get => types[i];
set => types[i] = value;
}
}
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Learn how to implement read-write, read-only, and write-only class properties using property accessors and access modifiers, and how to implement methods and extension methods for a class.