concept async method in category c#

This is an excerpt from Manning's book C# in Depth, Fourth Edition.
Async methods produce a result representing an asynchronous operation with no effort on the part of the developer. This result type is usually Task or Task<T>. Async methods use await expressions to consume asynchronous operations. If the method tries to await an operation that hasn’t completed yet, the method pauses asynchronously until the operation completes and then continues.
Like yield return, restrictions limit where you can use await expressions. The most obvious restriction is that you can use them only in async methods and async anonymous functions (which you’ll look at in section 5.7). Even within async methods, you can’t use the await operator within an anonymous function unless that’s async, too.
The await operator also isn’t allowed within an unsafe context. That doesn’t mean you can’t use unsafe code within an async method; you just can’t use the await operator within that part. The following listing shows a contrived example in which a pointer is used to iterate over the characters in a string to find the total of the UTF-16 code units in that string. It doesn’t do anything truly useful, but it demonstrates the use of an unsafe context within an async method.
Listing 5.3. Using unsafe code in an async method
static async Task DelayWithResultOfUnsafeCode(string text) { int total = 0; unsafe #1 { fixed (char* textPointer = text) { char* p = textPointer; while (*p != 0) { total += *p; p++; } } } Console.WriteLine("Delaying for " + total + "ms"); await Task.Delay(total); #2 Console.WriteLine("Delay complete"); }
Although the return type of async methods is fairly tightly restricted, most other aspects are as normal: async methods can be generic, static or nonstatic, and specify any of the regular access modifiers. Restrictions exist on the parameters you can use, however.

This is an excerpt from Manning's book C# in Depth.
The async method is GetPageLengthAsync.
Just like iterator blocks, this is a bit of a pain in terms of argument validation. Suppose you want to do some work in an async method after validating that the parameters don’t have null values. If you validate the parameters as you would in a normal synchronous code, the caller won’t have any indication of the problem until the task is awaited. The following listing gives an example of this.
Listing 15.5. Broken argument validation in an async method
static async Task MainAsync() { Task<int> task = ComputeLengthAsync(null); #1 Console.WriteLine("Fetched the task"); int length = await task; #2 Console.WriteLine("Length: {0}", length); } static async Task<int> ComputeLengthAsync(string text) { if (text == null) { throw new ArgumentNullException("text"); #3 } await Task.Delay(500); #4 return text.Length; } #1 – Deliberately passes bad argument #2 – 1 Awaits the result #3 – 2 Throws exception immediately #4 – Simulates real asynchronous work !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[]"} !@%STYLE%@!