Solution: Fastest Server Matchmaking

Review how to use Task.WhenAny to select the first completed asynchronous operation inside a utility class.

Solution: Fastest Server Matchmaking

Review how to use Task.WhenAny to select the first completed asynchronous operation inside a utility class.
C# 14.0
using System.Threading.Tasks;
namespace Multiplayer;
public class Matchmaker
{
public async Task<string> FindFastestServerAsync()
{
Task<string> usTask = PingServerAsync("US-East", 700);
Task<string> euTask = PingServerAsync("Europe", 500);
Task<string> asiaTask = PingServerAsync("Asia", 550);
Task<string> winningTask = await Task.WhenAny(usTask, euTask, asiaTask);
string fastestServer = await winningTask;
return fastestServer;
}
private async Task<string> PingServerAsync(string region, int delayInMilliseconds)
{
await Task.Delay(delayInMilliseconds);
return region;
}
}