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.");
}
}
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;
}
}
}