Skip to content

Instantly share code, notes, and snippets.

@binki
Last active November 5, 2016 14:51
Show Gist options
  • Save binki/d23857474627d6b3d9bb39cc40818114 to your computer and use it in GitHub Desktop.
Save binki/d23857474627d6b3d9bb39cc40818114 to your computer and use it in GitHub Desktop.
#csharp7 #tuple #destructuring
It took 00:00:01.0074966 to calculate hi.
using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace ValueTupleFun
{
class Program
{
static void Main(string[] args)
{
(var result, var time) = TestTuple(
async () =>
{
await Task.Delay(TimeSpan.FromSeconds(1));
return "hi";
}).Result;
Console.WriteLine($"It took {time} to calculate {result}.");
}
static async Task<(TResult result, TimeSpan time)> TestTuple<TResult>(
Func<Task<TResult>> actionAsync)
{
var chronometer = Stopwatch.StartNew();
return (await actionAsync(), chronometer.Elapsed);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment