It is called chainable methods.
Method chaining, also known as named parameter idiom, is a common
technique for invoking multiple method calls in object-oriented
programming languages. Each method returns an object (possibly the
current object itself), allowing the calls to be chained together in a
single statement.
Basicly, your method should return a current instance of your object.
public YourClass Add()
{
return this;
}
For a clean understanding of method chaining, here is the code converted from Java include in wikipedia page. The the setters return "this
" (the current Person
object).
using System;
namespace ProgramConsole
{
public class Program
{
public static void Main(string[] args)
{
Person person = new Person();
// Output: Hello, my name is Soner and I am 24 years old.
person.setName("Soner").setAge(24).introduce();
}
}
class Person
{
private String name;
private int age;
public Person setName(String name)
{
this.name = name;
return this;
}
public Person setAge(int age)
{
this.age = age;
return this;
}
public void introduce() {
Console.WriteLine("Hello, my name is " + name + " and I am " + age + " years old.");
}
}
}