0Pricing
C# Academy · Lesson

Expression-Bodied Members

Write compact properties and methods.

Expression-Bodied Members is a free C# Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

A More Concise Syntax

The expression-bodied syntax uses => to define a member as a single expression, replacing a full { ... } body. It makes short members compact and readable.

using System;

class Greeter
{
    public string Hello() => "Hi there!";
}

class Program
{
    static void Main()
    {
        var g = new Greeter();
        Console.WriteLine(g.Hello());
    }
}

Expression-Bodied Methods

Any method whose body is a single expression can use =>. The expression result becomes the return value.

using System;

class MathHelper
{
    public int Square(int x) => x * x;
    public int Add(int a, int b) => a + b;
}

class Program
{
    static void Main()
    {
        var m = new MathHelper();
        Console.WriteLine(m.Square(6));
        Console.WriteLine(m.Add(3, 4));
    }
}

Expression-Bodied Read-Only Properties

A computed read-only property can use => instead of get { return ...; }.

using System;

class Rectangle
{
    public double Width { get; set; }
    public double Height { get; set; }
    public double Area => Width * Height;
}

class Program
{
    static void Main()
    {
        var r = new Rectangle { Width = 4, Height = 5 };
        Console.WriteLine("Area: " + r.Area);
    }
}

Expression-Bodied get and set

You can also give the get and set accessors their own expression bodies, useful with a backing field.

using System;

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

class Program
{
    static void Main()
    {
        var p = new Person();
        p.Name = "  Niki  ";
        Console.WriteLine("[" + p.Name + "]");
    }
}

Returning Computed Strings

Expression-bodied properties shine for formatted, derived values like full names or labels.

using System;

class Customer
{
    public string First { get; set; }
    public string Last { get; set; }
    public string Display => First + " " + Last;
}

class Program
{
    static void Main()
    {
        var c = new Customer { First = "Ada", Last = "Lovelace" };
        Console.WriteLine(c.Display);
    }
}

Expression-Bodied with Conditions

The single expression can include a conditional (ternary) operator, keeping branching logic on one line.

using System;

class Account
{
    public decimal Balance { get; set; }
    public string Status => Balance >= 0 ? "OK" : "Overdrawn";
}

class Program
{
    static void Main()
    {
        var a = new Account { Balance = -20m };
        Console.WriteLine(a.Status);
    }
}

Void Expression-Bodied Methods

A method that performs an action (returns nothing) can also be expression-bodied if the action is a single statement.

using System;

class Logger
{
    public void Log(string msg) => Console.WriteLine("[LOG] " + msg);
}

class Program
{
    static void Main()
    {
        var log = new Logger();
        log.Log("Started");
    }
}

Expression-Bodied Constructors

A constructor with one assignment can use the arrow syntax too.

using System;

class Point
{
    public int X { get; }
    public Point(int x) => X = x;
}

class Program
{
    static void Main()
    {
        var p = new Point(7);
        Console.WriteLine("X = " + p.X);
    }
}

When NOT to Use It

Expression bodies are only for single expressions. If you need multiple statements, loops, or validation branches, use a normal block body instead.

using System;

class Calculator
{
    // Single expression -> arrow is fine
    public int Triple(int n) => n * 3;

    // Multiple statements -> use a block
    public int SumTo(int n)
    {
        int total = 0;
        for (int i = 1; i <= n; i++) total += i;
        return total;
    }
}

class Program
{
    static void Main()
    {
        var c = new Calculator();
        Console.WriteLine(c.Triple(4));
        Console.WriteLine(c.SumTo(5));
    }
}

Combining Several in One Class

A small class can use arrow syntax for nearly every member, dramatically reducing boilerplate.

using System;

class Temperature
{
    public double Celsius { get; set; }
    public double Fahrenheit => Celsius * 9 / 5 + 32;
    public double Kelvin => Celsius + 273.15;
    public override string ToString() => Celsius + "C";
}

class Program
{
    static void Main()
    {
        var t = new Temperature { Celsius = 25 };
        Console.WriteLine(t + " = " + t.Fahrenheit + "F = " + t.Kelvin + "K");
    }
}

Putting It Together

Use expression-bodied members to keep simple getters, helpers, and one-line methods clean while reserving block bodies for real logic.

using System;

class Order
{
    public int Quantity { get; set; }
    public decimal UnitPrice { get; set; }
    public decimal Total => Quantity * UnitPrice;
    public bool IsLarge => Quantity > 10;
}

class Program
{
    static void Main()
    {
        var o = new Order { Quantity = 12, UnitPrice = 9.99m };
        Console.WriteLine("Total: " + o.Total + ", Large: " + o.IsLarge);
    }
}

Quick Check

Test your understanding of expression-bodied members.

Recap

Expression-bodied members use => to define a member with a single expression. They work for methods, read-only properties, individual get/set accessors, and even constructors. Use them for concise one-liners; switch to a block body whenever you need multiple statements.

using System;

class Demo
{
    public int N { get; set; }
    public int Doubled => N * 2;
    public string Label() => "N is " + N;
}

class Program
{
    static void Main()
    {
        var d = new Demo { N = 9 };
        Console.WriteLine(d.Label() + ", doubled = " + d.Doubled);
    }
}

Frequently asked questions

Is the “Expression-Bodied Members” lesson free?

Yes — the full text of “Expression-Bodied Members” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Expression-Bodied Members”?

Write compact properties and methods. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Expression-Bodied Members” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Auto-Implemented Properties
  2. Full Properties with Backing Fields
  3. Expression-Bodied Members
  4. Indexers
← Back to C# Academy