jeudi 14 octobre 2021

The builder pattern, delegates and lambda expression- need explanation

I am not new in the programming world, however I need help with understanding what is going on in this code. I DO understand the builder pattern and the Action delagete (does not return anything, but can take some parameters).

What bothers me a lot is below code:

    0. var carBuilder = new CarBuilder().WithDoors(x => x.WithDoors(4)).Build();
    1. public CarBuilder WithDoors(Action<DoorsBuilder> x)
    2. {
    3.    var doorsBuilder = new DoorsBuilder();
    4.    x(doorsBuilder);
    5.    _car.AddDoors(doorsBuilder.Build());
    6.    return this;
    7. }
  1. What happens in line 4?
  2. What do I actually pass in line 0 to WithDoors function?
  3. Is is possible to rewrite that code in some simpler way (to get better understanding :))?

Code: https://rextester.com/SIOV54215

Aucun commentaire:

Enregistrer un commentaire