'How can I collect information about a variable that is inside a function from outside the function?

I made a script that calculates the delay between when a message appears and when the user interacts with it. I run the function within a for loop 8 times and I want to get the result of the delay (which is stored in one variable everytime the code runs) and use it to calculate an average.

using System.Diagnostics;
using System;



float userDelay;
float appearTime;
float userClickTime;

async Task<float> Start()
{
Random random = new Random();
int appearDelay = random.Next(100, 2000);

await Task.Delay(appearDelay);

Console.WriteLine("Click now!");


var time = Stopwatch.StartNew();

Console.ReadKey();
time.Stop();
userDelay = time.ElapsedMilliseconds;
Console.WriteLine("Your delay is exactly :" + userDelay + " Milliseconds");
Console.WriteLine();

return userDelay;

}

Console.WriteLine("Press any key to begin");
Console.ReadKey();

for (int i = 0; i < 8; i++)
{ 
await Start();

float[] userResults = { };

}

Is there a way I can do this?



Solution 1:[1]

Start is returning the value, so is easy to sum up all values and divide by the 8 times the call is done insitde the loop

float addedUpDelay = 0; 
for (int i = 0; i < 8; i++)
{ 
    addedUpDelay  += await Start();

    //float[] userResults = { };

}
Console.WriteLine("average: . . . . . . . . {0}", addedUpDelay / 8);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 J.Salas