// APM example
FileStream fileStream = new FileStream("file.txt", FileMode.Open);
byte[] buffer = new byte[1024];
fileStream.BeginRead(buffer, 0, buffer.Length, ar =>
{
int bytesRead = fileStream.EndRead(ar);
// Process data here
fileStream.Close();
}, null);
// TAP example
async Task ReadFileAsync()
{
using (FileStream fileStream = new FileStream("file.txt", FileMode.Open))
{
byte[] buffer = new byte[1024];
int bytesRead = await fileStream.ReadAsync(buffer, 0, buffer.Length);
// Process data here
}
}
async Task<int> PerformAsyncOperation()
{
await Task.Delay(1000);
return 42;
}
async Task MainAsync()
{
int result = await PerformAsyncOperation();
Console.WriteLine("Result: " + result);
}