Menu
Coddy logo textTech

Expression-Bodied Members

Part of the Object Oriented Programming section of Coddy's C# journey — lesson 14 of 70.

Expression-bodied members provide a concise syntax for members that contain only a single expression. Instead of writing full method or property bodies with braces, you use the => arrow syntax.

For methods that return a single expression, you can replace the entire body:

// Traditional method
public int Add(int a, int b)
{
    return a + b;
}

// Expression-bodied method
public int Add(int a, int b) => a + b;

This syntax works for read-only properties too, which you've already seen briefly:

public class Circle
{
    public double Radius { get; set; }
    public double Area => Math.PI * Radius * Radius;
    public double Circumference => 2 * Math.PI * Radius;
}

You can also use expression bodies for property getters and setters individually:

private string name;
public string Name
{
    get => name;
    set => name = value.Trim();
}

Even constructors can use this syntax when they contain a single statement:

public class Person
{
    public string Name { get; }
    public Person(string name) => Name = name;
}

Expression-bodied members make your code more readable when the logic is simple. Use them for straightforward operations, but stick with traditional bodies when you need multiple statements or complex logic.

challenge icon

Challenge

Easy

Let's build a product pricing calculator that showcases the power of expression-bodied members to write clean, concise code.

You'll create two files to organize your code:

  • Product.cs: Define a Product class in the Store namespace. This class should demonstrate various expression-bodied members:
    • An auto-implemented property Name (string) and BasePrice (decimal)
    • An expression-bodied constructor that sets both properties
    • An expression-bodied read-only property TaxAmount that calculates 10% of the base price
    • An expression-bodied read-only property TotalPrice that returns the base price plus tax amount
    • An expression-bodied method GetDiscount(decimal percentage) that returns the discount amount (base price multiplied by percentage divided by 100)
    • An expression-bodied method GetFinalPrice(decimal discountPercentage) that returns the total price minus the discount
  • Program.cs: In your main file, create a Product using input values, then display its pricing breakdown.

You will receive three inputs:

  • Product name
  • Base price
  • Discount percentage to apply

Print the pricing details in this format:

Product: {Name}
Base Price: {BasePrice}
Tax (10%): {TaxAmount}
Total Price: {TotalPrice}
Discount ({percentage}%): {discountAmount}
Final Price: {finalPrice}

For example, if the inputs are Laptop, 1000, and 15, the output should be:

Product: Laptop
Base Price: 1000
Tax (10%): 100
Total Price: 1100
Discount (15%): 150
Final Price: 950

Remember to use the => arrow syntax for all members that contain a single expression. This keeps your code clean and readable!

Cheat sheet

Expression-bodied members use the => arrow syntax for members containing a single expression, providing a concise alternative to traditional method and property bodies.

Expression-bodied methods:

// Traditional method
public int Add(int a, int b)
{
    return a + b;
}

// Expression-bodied method
public int Add(int a, int b) => a + b;

Expression-bodied read-only properties:

public class Circle
{
    public double Radius { get; set; }
    public double Area => Math.PI * Radius * Radius;
    public double Circumference => 2 * Math.PI * Radius;
}

Expression-bodied property getters and setters:

private string name;
public string Name
{
    get => name;
    set => name = value.Trim();
}

Expression-bodied constructors:

public class Person
{
    public string Name { get; }
    public Person(string name) => Name = name;
}

Use expression-bodied members for straightforward operations. For multiple statements or complex logic, use traditional bodies.

Try it yourself

using System;
using Store;

class Program
{
    public static void Main(string[] args)
    {
        // Read inputs
        string name = Console.ReadLine();
        decimal basePrice = Convert.ToDecimal(Console.ReadLine());
        decimal discountPercentage = Convert.ToDecimal(Console.ReadLine());

        // TODO: Create a Product object using the input values

        // TODO: Calculate the discount amount using GetDiscount method

        // TODO: Calculate the final price using GetFinalPrice method

        // TODO: Print the pricing details in the required format:
        // Product: {Name}
        // Base Price: {BasePrice}
        // Tax (10%): {TaxAmount}
        // Total Price: {TotalPrice}
        // Discount ({percentage}%): {discountAmount}
        // Final Price: {finalPrice}
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming