C# 7.0 standalone discard confusion

喜欢而已 提交于 2021-02-07 12:23:30

问题


I would like to better understand a couple of examples involving the usage of C# 7.0 discard feature.

Both of them make use of the so called stand alone discard.

This is the first example confusing me, which is available here:

public class EmailController
{
    public ActionResult SendEmail(string email)
    {
        var correlationId = HttpContext.Request.Headers["x-correlation-id"].ToString();

        // Starts sending an email, but doesn't wait for it to complete
        _ = SendEmailCore(correlationId);
        return View();
    }

    private async Task SendEmailCore(string correlationId)
    {
        // send the email
    }
}

Is the standalone discard used when calling SendEmailCore useful in any way ? What's the difference between the code above and the same code without the assignment of SendEmailCore(correlationId) to the discard expression _ ? Is there any difference in the runtime behavior ?

Here is the second code example, which is available here:

using System;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      ExecuteAsyncMethods().Wait();
   }

   private static async Task ExecuteAsyncMethods()
   {    
      Console.WriteLine("About to launch a task...");
      _ = Task.Run(() => { var iterations = 0;  
                           for (int ctr = 0; ctr < int.MaxValue; ctr++)
                              iterations++;
                           Console.WriteLine("Completed looping operation...");
                           throw new InvalidOperationException();
                         });
      await Task.Delay(5000);                        
      Console.WriteLine("Exiting after 5 second delay");
   }
}

Again, I can't see the usefulness of the assignment to the discard expression _, I tried the example in visual studio, but it seems that the runtime behavior is the same with and without the discard expression. Furthermore, in the example description there is the following sentence that confuses me even more:

The following example uses a standalone discard to ignore the Task object returned by an asynchronous operation. This has the effect of suppressing the exception that the operation throws as it is about to complete.

The exception raised inside the call to Task.Run is ignored by the runtime in any case, in the sense that the program completes successfully with or without the discard expression.

This is not related to the discard expression itself, but to the fact that the returned task is abandoned and its exception is not observed. The task is not awaited and there is not a call to Task.Wait, so the CLR ignores the exception and the process doesn't crash; this is the usual Task behavior since .NET 4.5. So what is the point of the quoted sentence ?

来源:https://stackoverflow.com/questions/56875049/c-sharp-7-0-standalone-discard-confusion

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!