Lazy Argument Validation - CSharp Thread Asynchronous

CSharp examples for Thread Asynchronous:Task

Description

Lazy Argument Validation

Demo Code

using System;// w ww .  j  av a2 s .  c om
using System.ComponentModel;
using System.Threading.Tasks;
class LazyArgumentValidation
{
   static void Main()
   {
      MainAsync().GetAwaiter().GetResult();
   }
   static async Task MainAsync()
   {
      Task<int> task = ComputeLengthAsync(null);
      Console.WriteLine("Fetched the task");
      int length = await task;
      Console.WriteLine("Length: {0}", length);
   }
   static async Task<int> ComputeLengthAsync(string text)
   {
      if (text == null)
      {
         throw new ArgumentNullException("text");
      }
      await Task.Delay(500);
      return text.Length;
   }
}

Result


Related Tutorials