Select in C#
C# doesn’t have a direct equivalent to Go’s select statement for channel operations. However, we can achieve similar functionality using Tasks and async/await. Here’s how we can translate the concept:
using System;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
// For our example we'll use two tasks instead of channels.
var task1 = Task.Run(async () =>
{
await Task.Delay(1000);
return "one";
});
var task2 = Task.Run(async () =>
{
await Task.Delay(2000);
return "two";
});
// We'll use Task.WhenAny to await both of these values
// simultaneously, printing each one as it completes.
for (int i = 0; i < 2; i++)
{
var completedTask = await Task.WhenAny(task1, task2);
if (completedTask == task1)
{
Console.WriteLine($"received {await task1}");
task1 = Task.Delay(-1); // This task is completed, replace it with a never-completing task
}
else
{
Console.WriteLine($"received {await task2}");
task2 = Task.Delay(-1); // This task is completed, replace it with a never-completing task
}
}
}
}In this C# version:
We use
Tasks instead of channels to represent asynchronous operations.Each task simulates a blocking operation by using
Task.Delay().We use
Task.WhenAny()to wait for either task to complete, which is similar to theselectstatement in the original example.We use a
forloop to process both tasks, similar to the original example.After a task completes, we replace it with a never-completing task (
Task.Delay(-1)) to ensure it’s not selected again in the next iteration.
To run the program, save it as Select.cs and use the dotnet CLI:
$ dotnet run Select.cs
received one
received twoNote that the total execution time is only ~2 seconds since both the 1 and 2 second delays execute concurrently.
This example demonstrates how to work with multiple asynchronous operations in C#, waiting for them to complete in any order. While it’s not identical to Go’s select, it achieves a similar purpose of handling concurrent operations efficiently.