Expression-bodied members (C# programming guide)
Expression body definitions let you provide a member's implementation in a very 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:
member => expression;
where expression is a valid expression.
Support for expression body definitions was introduced for methods and read-only properties in C# 6 and was expanded in C# 7.0. Expression body definitions can be used with the type members listed in the following table:
Member | Supported as of... |
---|---|
Method | C# 6 |
Read-only property | C# 6 |
Property | C# 7.0 |
Constructor | C# 7.0 |
Finalizer | C# 7.0 |
Indexer | C# 7.0 |
Methods
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. Note that the return
keyword is not used in the ToString
expression body definition.
using System;
public class Person
{
public Person(string firstName, string lastName)
{
fname = firstName;
lname = lastName;
}
private string fname;
private string lname;
public override string ToString() => $"{fname} {lname}".Trim();
public void DisplayName() => Console.WriteLine(ToString());
}
class Example
{
static void Main()
{
Person p = new Person("Mandy", "Dejesus");
Console.WriteLine(p);
p.DisplayName();
}
}
For more information, see Methods (C# Programming Guide).
Read-only properties
Starting with C# 6, you can use expression body definition to implement a read-only property. To do that, use the following syntax:
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:
public class Location
{
private string locationName;
public Location(string name)
{
locationName = name;
}
public string Name => locationName;
}
For more information about properties, see Properties (C# Programming Guide).
Properties
Starting with C# 7.0, you can use expression body definitions to implement property get
and set
accessors. The following example demonstrates how to do that:
public class Location
{
private string locationName;
public Location(string name) => Name = name;
public string Name
{
get => locationName;
set => locationName = value;
}
}
For more information about properties, see Properties (C# Programming Guide).
Constructors
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.
public class Location
{
private string locationName;
public Location(string name) => Name = name;
public string Name
{
get => locationName;
set => locationName = value;
}
}
For more information, see Constructors (C# Programming Guide).
Finalizers
An expression body definition for a finalizer typically contains cleanup statements, such as statements that release unmanaged resources.
The following example defines a finalizer that uses an expression body definition to indicate that the finalizer has been called.
using System;
public class Destroyer
{
public override string ToString() => GetType().Name;
~Destroyer() => Console.WriteLine($"The {ToString()} destructor is executing.");
}
For more information, see Finalizers (C# Programming Guide).
Indexers
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 a number of sports. Both the indexer get
and set
accessors are implemented as expression body definitions.
using System;
using System.Collections.Generic;
public class Sports
{
private string[] types = { "Baseball", "Basketball", "Football",
"Hockey", "Soccer", "Tennis",
"Volleyball" };
public string this[int i]
{
get => types[i];
set => types[i] = value;
}
}
For more information, see Indexers (C# Programming Guide).