Hi, Shervan360
Sum() is calling method.
Main is caller.
To distinguish between caller and called.
CallerMemberNameAttribute Class allows you to obtain the method or property name of the caller to the method.
using System.Dynamic;
using System.Runtime.CompilerServices;
namespace ConsoleApp11
{
internal class Program
{
static long Sum([CallerMemberName] string callerName = "") //Is it caller, calling or called method?
{
Console.WriteLine(callerName + " called Sum.");
long sum = 0;
for (int i = 0; i < 10000; i++)
{
sum += i;
}
return sum;
}
static async Task<long> Calculate([CallerMemberName] string callerName = "") //Is it caller, calling or called method?
{
Console.WriteLine(callerName + " called Calculate.");
var result = await Task.Run(() => Sum());
return result;
}
static void Main(string[] args)// Is it caller, calling or called method?
{
Console.WriteLine(Calculate().Result);
Console.ReadKey();
}
}
}
Best regards,
Minxin Yu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.