Beginner - Method Chaining
I was talking to a recent bootcamp graduate whose course hadn't covered this, so let's go through an example.
Scenario
Jim is making a sandwich. He already knows which ingredients he'll take from the fridge:
- Mayonnaise
- Lettuce
- Cheese
- Ham
Jim has to go to the fridge for each ingredient. Each trip needs a reference to the fridge and removes one item.
Could Jim make his sandwich more efficiently?
As code, it looks something like this:
var fridge = new Fridge();
fridge.RemoveMayonnaise();
fridge.RemoveCheese();
fridge.RemoveHam();
fridge.RemoveLettuce();Figure 1: the fridge, and removing items
Calling several methods on one object like this is very common in object-oriented programming. It's perfectly valid, but it has some issues:
- We re-reference the object again and again, typing
fridgeand invoking a method on every line. - It adds noise and lines.
Clear as it is, nothing relates the calls to each other. Each one is completely independent. Think of them as a series of cogs.

In the code, the cogs sit one after another, but they could easily be separated, and it's hard to keep them in order. Now imagine them linking together like a... chain! There's a specific order, and we can even enforce one, but that's a future topic.
Let's look at how method chaining helps.
If you've used anything from the System.Linq namespace,
you've almost certainly used method chaining:
var people = peoples.Where(p => p.Age >= 30)
.OrderByDescending(p => p.Name)
.Take(10)
.ToList();Note: this example also uses fluent interfaces, which we'll get to another time.
Method chaining:
- forms a chain of commands tied to an object
- creates a sequence of optional processing steps, in no particular order
- makes calling code simpler and cleaner
Sounds good? Let's get started.
Improving the code in Figure 1
Method chaining has two requirements:
- Each method returns the type of the class.
- Each method ends by returning the current instance.
Here's RemoveMayonnaise with both changes:
public Fridge RemoveMayonnaise()
{
// implementation omitted for brevity
return this;
}- Line 1 declares the return type,
Fridge. - Line 4 returns the instance itself,
this.
Because each method returns the Fridge instance, the
next call can use any of its public methods. Welcome to method chaining.
Here are the rest of the methods:
class Program
{
static void Main(string[] args)
{
// ### Previous code
// var fridge = new Fridge();
// fridge.RemoveMayonnaise();
// fridge.RemoveCheese();
// fridge.RemoveHam();
// fridge.RemoveLettuce();
// ### New code
var fridge = new Fridge();
fridge.RemoveMayonnaise()
.RemoveHam()
.RemoveCheese()
.RemoveLettuce();
}
}
class Fridge
{
public Fridge RemoveMayonnaise()
{
// implementation omitted for brevity
return this;
}
public Fridge RemoveLettuce()
{
// implementation omitted for brevity
return this;
}
public Fridge RemoveCheese()
{
// implementation omitted for brevity
return this;
}
public Fridge RemoveHam()
{
// implementation omitted for brevity
return this;
}
}The calling code under ### New code is shorter, flows
better and reads much more cleanly than
### Previous code.
But wait...
Jim is back. The good news: thanks to method chaining, he can gather his ingredients in one trip to the fridge.
The bad news: his last sandwich taught him that lettuce on the bottom slice makes the bread soggy and threatens the sandwich's structural integrity. The ingredients need to go on in a specific order.
So how can we enforce a sequence?
That's the next post: fluent interfaces.