Method chaining
Method chaining is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results.
Method chaining is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results. ==Examples== ===Stream-based I/O=== A common example is std::istream and std::ostream (the input and output stream classes) in C++, where for example returns the left object, allowing chaining. a ===Functional programming=== Functional programming is another example of the usage of method chaining, involving higher-order functions. In object-oriented programming, higher-order functions and manipulation of collections can be represented through methods that are chained together. One such example in Java uses the built-in methods of java.util.stream.Stream : import java.util.Arrays; import java.util.List; import java.util.streams.Collectors; List numbers = Arrays.asList(10, 25, 30, 45, 60, 75, 90); List result = numbers.stream() // .stream() returns java.util.stream.Stream .filter(n -> n > 30) .map(n -> n * 2) .sorted() .collect(Collectors.toList()); Note that in Java, filter() , map() , and sorted() return a new shallow copy of the preceding list. However, to operate on the list in-place, the sort() method can be used. Language Integrated Query (or LINQ) for C# makes extensive usage of method chaining. using System; using System.Collections; // using method chaining: IEnumerble results = list .Where(x => x.Bar new {x.Bar, x.Qux}); results.ForEach(x => { Console.WriteLine(x.ToString()); }); // using LINQ keywords: IEnumerable results = from x in list where x.Bar C++20 utilized operator| as a "piping operator" for LINQ-style chaining operations with the std::ranges library. std::views contains several classes which are invoked through operator() . import std; using std::vector; using std::ranges::to; using std::views::filter; using std::views::transform; vector numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Pipeline: filter even numbers, double them, and then sum the result vector result = numbers | filter([](int n) -> bool { return n % 2 == 0; }) | transform([](int n) -> int { return n * 2; }) | to (); Similarly, the C++26 std::execution library (the execution or sender/receiver framework) builds asynchronous, data-parallel sender/receiver graphs through chaining of operator| : import std; using std::optional; using std::tuple; using std::execution::just; using std::execution::sender; using std::execution::then; sender auto work = just(6) | then([](int x) -> int { return x * 7; }) | then([](int x) -> int { std::println("Result after multiplying by 7: {}", x); return x; }); int answer = co_await std::move(work); ===Design patterns=== The Builder pattern relies on constructing an object through method calls rather than immediately in its constructor. For example, java.lang.StringBuilder makes use method chaining to build a java.util.StringBuilder , which is finally turned back to a java.util.String through a toString() call. String result = new StringBuilder() .append("Hello, ") .append("world!") .insert(0, "Greeting: ") .replace(10, 18, "beautiful ") .toString(); The Fluent interface pattern relies entirely on method chaining to implement method cascading. In C++, similar method chaining to Java and C# can be accomplished by having the methods return (for example, for a class StringBuilder , append() and replace() could have type StringBuilder& and return *this ). However, in C++ it is important to be conscious of potential object slicing if the returned reference is to a parent class, while other languages such as Java, C#, and Rust are not subject to this problem. ==See also== Fluent interface Pipeline (Unix) Nesting (computing) Builder pattern Pyramid of doom (programming) ==References== ==External links== Creating DSLs in Java using method chaining concept Method Chaining in PHP
References
No reference rows published for this article.
Cite this article
MLA
"Method chaining." encyclopegia.org, 2026, https://encyclopegia.org/wiki/method-chaining. Accessed 24 November 2026.
Chicago
"Method chaining." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/method-chaining.
APA
encyclopegia.org. (2026). Method chaining. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/method-chaining
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON