C# > Language Features by Version > C# 6 to C# 12 Highlights > Expression-bodied Members (C# 6)
Expression-bodied Members in C# 6: A Concise Syntax for Methods and Properties
C# 6 introduced expression-bodied members, a concise syntax for writing method and property implementations when they consist of a single expression. This feature enhances code readability and reduces boilerplate. This example explores the usage of expression-bodied members for methods and properties.
Basic Syntax and Example
This code demonstrates the basic syntax of expression-bodied members. The `=>` operator is used to define the member's implementation as a single expression. For the `FullName` property, the getter simply returns a formatted string. The `GetGreeting` method also returns a formatted string, illustrating how expression-bodied members can be used for methods with simple logic.
using System;
public class Person
{
private string _firstName;
private string _lastName;
public Person(string firstName, string lastName)
{
_firstName = firstName;
_lastName = lastName;
}
// Expression-bodied property getter
public string FullName => $"{_firstName} {_lastName}";
// Expression-bodied method
public string GetGreeting(string salutation) => $"{salutation}, {FullName}!";
public static void Main(string[] args)
{
Person person = new Person("John", "Doe");
Console.WriteLine(person.FullName); // Output: John Doe
Console.WriteLine(person.GetGreeting("Hello")); // Output: Hello, John Doe!
}
}
Concepts Behind the Snippet
Expression-bodied members are syntactic sugar provided by C# to simplify code. They are particularly useful when a method or property's implementation is straightforward and can be expressed as a single expression. This syntax reduces the need for curly braces and the `return` keyword, leading to cleaner and more readable code. The compiler implicitly understands that the expression's result should be returned.
Real-Life Use Case
Expression-bodied members are valuable in scenarios where you have simple getter properties or methods that perform a single calculation or transformation. For example, calculating the area of a circle, formatting a string, or retrieving a value from a cache. In data transfer objects (DTOs) or simple data models, they significantly improve code clarity.
Best Practices
Use expression-bodied members judiciously. They are most effective when the implementation is a single, easily understandable expression. Avoid using them for complex logic that would make the expression difficult to read or debug. Always prioritize readability over brevity if there's a trade-off.
Interview Tip
Be prepared to explain the benefits of expression-bodied members, such as improved readability and reduced boilerplate. Also, be aware of when they are appropriate (simple, single-expression logic) and when they are not (complex logic requiring multiple statements or branches).
When to Use Them
Use expression-bodied members when:
Alternatives
Before C# 6, you would typically write the same code using a traditional method or property syntax with curly braces and a `return` statement. While this alternative works, it's more verbose. Another alternative in very simple cases (property getters) is to use automatically implemented properties, but this doesn't apply to methods or properties that need to perform any computation or transformation.
Pros
Cons
FAQ
-
Can I use expression-bodied members for methods with multiple statements?
No, expression-bodied members are designed for single-expression implementations. For methods with multiple statements, you need to use the traditional method syntax with curly braces. -
Can I use expression-bodied members for property setters?
Yes, property setters can also be expression-bodied since C# 7.0. For example: `public string Name { get => _name; set => _name = value; }`