liveBook · Manning

Method chaining

As explained in .NET in Action, Second Edition

Method chaining is a programming style in which successive method calls are applied to the object returned by the preceding call.

Method chaining applies successive method calls to the object returned by the preceding call. In command-line parsing, WithParsed runs first; WithNotParsed then examines the same ParserResult. section 2.8

Conditional command-line handling

Parser.Default.ParseArguments<Options>(args) .WithParsed<Options>(AsciiArt.Write) .WithNotParsed(_ => WriteLine("Usage: HelloDotnet <text> --font Big")); WithParsed executes the success action when parsing succeeds; otherwise, it returns the ParserResult so WithNotParsed can handle failure. section 2.8

Listing 2.16 Program.cs parsing command-line options with CommandLineParser
using HelloDotnet;
using CommandLine;                                          #1
  
Parser.Default.ParseArguments<Options>(args)                #2
  .WithParsed<Options>(AsciiArt.Write)                      #3
  .WithNotParsed(_ =>                                       #4
    WriteLine("Usage: HelloDotnet <text> --font Big"));     #5

Inspect the complete C# command-line parsing chain and its lambda-based failure action. section 2.8

How .NET in Action, Second Edition covers it

How deep it goes
Explains return-value flow and conditional execution, with a focused C# implementation rather than general chaining internals.
Examples
Demonstrates ParseArguments followed by WithParsed and WithNotParsed in a console application.
What it assumes
Assumes familiarity with C# method calls, delegates, and generic methods while demonstrating lambda callbacks in context.
What you can do after
Apply it via CommandLineParser to route successful and unsuccessful parsing results through concise callback chains.
How it connects
Connects method chaining with extension methods, lambda expressions, anonymous functions, and command-line parsing.

Also explained in

Compare how 3 books explain method chaining →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.