0% found this document useful (0 votes)
3 views3 pages

Async - Code Snippets

The document explains the difference between synchronous and asynchronous programming using C# code snippets. It illustrates how asynchronous programming allows methods to run concurrently without waiting for each other to complete, improving efficiency. Additionally, it provides an example output of a program utilizing async and await keywords, demonstrating the order of execution in asynchronous calls.

Uploaded by

Merlin
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Async - Code Snippets

The document explains the difference between synchronous and asynchronous programming using C# code snippets. It illustrates how asynchronous programming allows methods to run concurrently without waiting for each other to complete, improving efficiency. Additionally, it provides an example output of a program utilizing async and await keywords, demonstrating the order of execution in asynchronous calls.

Uploaded by

Merlin
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Asynchronous Programming -

Code Snippets
Difference between Synchronous and Asynchronous programming

In below code – code 1.0, Method2 needs to wait for Method1 to complete execution. To solve this
problem, we use async programming with the help of Task in code 1.1

code 1.0 - Synchronous Programming


using System;
using [Link];
using [Link];
public class HelloWorld
{
public static void Main(string[] args)
{
[Link]("Start");

[Link](Method1());
[Link](Method2());
[Link](Method3());

[Link]("End");
}

public static int Method1()


{
[Link](50000);
return 10;
}

public static int Method2()


{
return 20;
}

public static int Method3()


{
return 30;
}

code 1.1 - Asynchronous Programming


using System;
using [Link];
using [Link];
public class HelloWorld
{
public static void Main(string[] args)
{
[Link]("Start");

Task t1 = [Link](() => [Link](Method1()));


Task t2 = [Link](() => [Link](Method2()));
Task t3 = [Link](() => [Link](Method3()));

[Link]();
[Link]("End");
}

public static int Method1()


{
[Link](500);
return 10;
}

public static int Method2()


{
return 20;
}

public static int Method3()


{
return 30;
}

Find out the output for the below code:

// Online C# Editor for free


// Write, Edit and Run your C# code using C# Online Compiler

using System;
using [Link];
public class HelloWorld
{
public static void Main(string[] args)
{
[Link] ("Main start");
Method1();
[Link]("Main end");
}
public static async void Method1()
{
[Link]("Method1 start");
await Method2();
[Link]("Method1 end");
}

public static async Task Method2()


{
[Link]("Method2 start");
await [Link](1000);
[Link]("Method2 end");
}
}

Output:

Main start
Method1 start
Method2 start
Main end
Method2 end
Method1 end
Most asked Async and Await output based interview questions and answers #coding #shorts
#csharp

Reference

What is the role of Async and Await ?

You might also like