D
D
Denis Bredun2020-08-08 02:51:23
ASP.NET
Denis Bredun, 2020-08-08 02:51:23

Why in a web application, on a page, 8 is displayed, and not 4?

The code for the Configure method of the Startup class:

public void Configure(IApplicationBuilder app)
    {
      int x = 2;
      app.Run(async (context) =>
      {
        x = x * 2;  //  2 * 2 = 4
        await context.Response.WriteAsync($"Result: {x}");
      });
    }

In theory, we expect 4, but the page displays 8: https://metanit.com/sharp/aspnet5/pics/middlewarel...
Why? Is it related to async? Or with how many requests the browser sends?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
none7, 2020-08-08
@none7

This is because the anonymous method is called twice. Once for logs, the second for sending to the client. Don't write anything to parent method variables.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question