liveBook · Manning

Generics

As explained in .NET in Action, Second Edition

A generic type or method is parameterized by one or more types so the same definition can operate with different type arguments.

A generic method accepts a type argument, letting its signature use a type chosen at the call site. In command-line parsing, `ParseArguments<Options>` supplies the options type, while `IEnumerable<string>` specifies a sequence of strings. The parser can then return a `ParserResult<T>` for the chosen type. 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

The call supplies `Options` as the type argument to `ParseArguments` and `WithParsed`. section 2.8

How .NET in Action, Second Edition covers it

How deep it goes
Defines generic arguments through method signatures and a focused parsing example.
Examples
Uses ParseArguments<Options> and IEnumerable<string> in command-line parsing.
What you can do after
Apply generics via CommandLineParser to parse arguments into a chosen options type.
How it connects
Connects generic arguments to IEnumerable, ParserResult, and typed command-line parsing.

Also explained in

Compare how 20 books explain generics →

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.