-
Notifications
You must be signed in to change notification settings - Fork 5
Asynchronous Programming
Sann Lynn Htun edited this page Nov 19, 2024
·
1 revision
Asynchronous programming in C# allows tasks to run concurrently, improving performance and responsiveness, especially for I/O-bound operations.
- async and await Keywords
Enable asynchronous methods and task completion without blocking the main thread.
using System;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Starting asynchronous operation...");
await PerformAsyncOperation();
Console.WriteLine("Asynchronous operation completed.");
}
static async Task PerformAsyncOperation()
{
Console.WriteLine("Working...");
await Task.Delay(2000); // Simulates a delay
Console.WriteLine("Done.");
}
}
using System;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
int result = await CalculateAsync();
Console.WriteLine($"Calculation result: {result}");
}
static async Task<int> CalculateAsync()
{
await Task.Delay(2000); // Simulates a delay
return 42; // Returns a result
}
}
using System;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
try
{
await PerformAsyncOperation();
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
}
static async Task PerformAsyncOperation()
{
await Task.Delay(2000);
throw new InvalidOperationException("An error occurred.");
}
}
- Task and Task
Represent asynchronous operations, with
Task<T>
returning a value of typeT
.
- Parallel Programming
Using
Task.WhenAll
andTask.WhenAny
to perform multiple tasks simultaneously.
- Asynchronous Streams
Use
IAsyncEnumerable<T>
andawait foreach
for asynchronous data streams.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
await foreach (var number in GenerateNumbersAsync())
{
Console.WriteLine(number);
}
}
static async IAsyncEnumerable<int> GenerateNumbersAsync()
{
for (int i = 1; i <= 5; i++)
{
await Task.Delay(1000); // Simulates delay
yield return i;
}
}
}
- async and await: Enable asynchronous operations.
- Task and Task: Handle asynchronous methods, optionally returning values.
- Exception Handling: Use try-catch for asynchronous methods.
- Advanced Techniques: Parallel tasks and asynchronous streams for efficient data handling.
C# asynchronous programming enhances application performance and responsiveness, making it ideal for modern, high-performance applications.
-
Introduction to C#
What is C#? -
Variables and Data Types
Understand how to declare and use variables. -
Operators
Arithmetic, relational, and logical operators in C#. -
Control Statements
If-else, switch, and looping constructs.
-
Classes and Objects
Basics of OOP in C#. -
Inheritance
How to use inheritance effectively. -
Polymorphism
Method overriding and overloading. -
Encapsulation
Understanding access modifiers.
-
LINQ Basics
Working with Language Integrated Query. -
Asynchronous Programming
Introduction to async/await. -
File Handling
Reading and writing files.
-
Number Formatting
Formatting numbers in C#. -
Exception Handling
Handling runtime errors effectively. -
Working with Dates
DateTime and time zone handling. -
Using Keyword in C#
Different usages of theusing
keyword in C#.
-
Setting Up Development Environment
Installing Visual Studio and .NET SDK. - Useful Libraries Libraries and tools for C# development.