async vs synchronous code example

Example 1: async vs sync

synchronous (sync) - you can only execute one thing at a time
asynchronous (async) - you can execute multiple things at the same time

Example 2: async await vs synchronous c#

Task getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
string urlContents = await getStringTask;
      
                 VS
string urlContents = await client.GetStringAsync();

Answer:
Calling await client.GetStringAsync() yields the execution to the calling method,
which means it won't wait for the method to finish executing,
and thus won't block the thread. Once it's done executing in the background,
the method will continue from where it stopped.

If you just call client.GetString(), the thread's execution won't continue
until this method finished executing,
which will block the thread and may cause the UI to become unresponsive.

Tags:

Misc Example