How to use fluent interfaces and method chaining in C#

When doing work in programs you might typically find that the supply code has develop into so complicated that it is tough to understand and keep. Fluent interfaces and technique chaining are two ideas that endeavor to make your code readable and uncomplicated. This short article examines fluent interfaces and […]

When doing work in programs you might typically find that the supply code has develop into so complicated that it is tough to understand and keep. Fluent interfaces and technique chaining are two ideas that endeavor to make your code readable and uncomplicated. This short article examines fluent interfaces and technique chaining and how you can do the job with them in C#.

To do the job with the code examples supplied in this short article, you should have Visible Studio 2019 mounted in your system. If you do not now have a copy, you can download Visible Studio 2019 in this article. 

Build a console software project in Visible Studio

Very first off, let us create a .Internet Main console software project in Visible Studio. Assuming Visible Studio 2019 is mounted in your system, adhere to the techniques outlined underneath to create a new .Internet Main console software project in Visible Studio.

  1. Launch the Visible Studio IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, pick “Console App (.Internet Main)” from the listing of templates exhibited.
  4. Click Up coming. 
  5. In the “Configure your new project” window, specify the title and locale for the new project.
  6. Click Build.

This will create a new .Internet Main console software project in Visible Studio 2019. We’ll use this project in the subsequent sections of this short article.

Fluent interfaces and technique chaining stated

Strategy chaining is a system in which strategies are known as on a sequence to type a chain and each individual of these strategies return an occasion of a course. These strategies can then be chained jointly so that they type a solitary assertion. A fluent interface is an object-oriented API that is dependent largely on technique chaining. The goal of a fluent interface is to lower code complexity, make the code readable, and create a area particular language (DSL). It is a form of technique chaining in which the context is taken care of using a chain.

You might now be using technique chaining in your programs, knowingly or unknowingly. The pursuing code snippet illustrates how strategies are chained.

Copyright © 2020 IDG Communications, Inc.

Rosa G. Rose

Next Post

2021 Mercedes-AMG GLE53 Coupe review: Astonishingly nimble

Tue Sep 8 , 2020
No matter if or not you like the appears, there is no denying the GLE53’s efficiency. Daniel Golson/Roadshow The 2021 Mercedes-AMG GLE53 Coupe is amazingly pleasurable, but I need to have to get the styling out of the way initial. No make any difference what your emotions are on crossover-coupes — […]