Called vs Calling vs Called Method in C#

Shervan360 1,481 Reputation points
2023-03-17T03:24:44.1233333+00:00

Could you please explain what is the difference between these Called vs Calling vs Called Method in C#?

Please write an example.

I'm reading Microsoft Learn (this) and I want to make sure the differences.

  • When the await keyword is applied, it suspends the calling method and yields control back to its caller until the awaited task is complete.

For example, in the following code, which one is Called, Caller and Calling method?

Thanks in advance

using System.Dynamic;

namespace ConsoleApp11
{
    internal class Program
    {
        static long Sum() //Is it caller, calling or called method?
        {
            long sum = 0;
            for (int i = 0; i < 10000; i++)
            {
                sum += i;
            }
            return sum;
        }
        static async Task<long> Calculate() //Is it caller, calling or called method?
        {
            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();
        }
    }
}
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,367 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Minxin Yu 10,031 Reputation points Microsoft Vendor
    2023-03-17T07:13:05.96+00:00

    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();
            }
        }
    }
    

    enter image description here

    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.

    0 comments No comments