C# 8 Ranges and Recursive Patterns

Source: InfoQ

This article discusses two features proposed for C# 8: Ranges and Recursive Patterns. Both of these belong to the category of Code Simplification. The article explains these in detail with many examples and how these features can help you to write better code.

Key Takeaways

  • C# 8 Adds Ranges and Recursive Patterns
  • Ranges easily define a sequence of data, replacing the Enumberable.Range()
  • Recursive Patterns brings an F#-like construct to C#
  • Recursive Patterns is an awesome feature, it giving you the flexibility to testing the data against a sequence of conditions and performing further computations based on the condition met.
  • Ranges is very useful to generate sequences of numbers in the form of a collection or a list.

Ranges easily define a sequence of data. It is a replacement for Enumerable.Range() except it defines the start and stop points rather than start and count and it helps you to write more readable code.

Recursive Patterns matching is a very powerful feature, which allows code to be written more elegantly, mainly when used together with recursion. The Recursive Patterns consists of multiple sub-patterns like as Positional Patterns, e.g.,var isBassam = user is Employee ("Bassam",_), Property Patterns, e.g.,p is Employee {Name is "Mais"}, Var Pattern, Discard Pattern (‘_’),and so forth.

 

Read the entire article at InfoQ.