Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8561041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:24:48+00:00 2026-06-11T16:24:48+00:00

Recently I try to learn about the new C# feature, async/await keywords for asynchronous

  • 0

Recently I try to learn about the new C# feature, async/await keywords for asynchronous programming. When I searched at web, I faced this example:

    static void Main(string[] args)
    {
        Console.WriteLine("Task based APM demo");

        // Call Exponnent() asynchronously.
        // And immediately return the control flow.
        // If I don't put a Task here, the program will sometimes
        // terminate immediately.
        Task t = new Task(async () =>
        {
            int result = await Program.Exponent(10);

            // After the operation is completed, the control flow will go here.
            Console.WriteLine(result);
        });

        t.Start();
        Console.ReadKey();
    }

    static async Task<int> Exponent(int n)
    {
        Console.WriteLine("Task started");
        return await TaskEx.Run<int>(() => 2 << (n - 1));
    }
}

I have questions about it and about the manner that this statements act.
First as I understand an await expression used when we want release the process at that point and return to the caller context. But why this expression used this at the line which the Exponent method called at it? In fact what happen when the compiler faced this line of program?
And the letter question is, why the program use “TaskEx.Run” for returning the result in the body of Exponent method? Is it possible to use “return await (() => 2 << (n – 1));” only? How the compiler behaves with this line?

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:24:49+00:00Added an answer on June 11, 2026 at 4:24 pm

    But why this expression used this at the line which the Exponent called at it?

    I’m not sure what you mean by this question. await Task.Run<int>(() => 2 << (n - 1)); tells the compiler to suspend execution of the current method until the task completes. In terms of task, this is similar to:

    Task.Run<int>(() => 2 << (n - 1)).ContinueWith(t=>return t.Result);
    

    But, of course, you cannot return from a continuation. The async keyword tells the compiler not only that there could be one or more await operators in the method, but set’s up a state machine to manage the different state transitions starting and returning from asynchronous operations. As well, this could affect how the method is executed in relationship to being asynchronous to the caller.

    As to the Task used in Main: you can’t await a method call in Main, so, I assume the Task is used to still use the await operator. But, it seems redundant. I would probably simply have another method to use await:

        static async void Method()
        {
            Console.WriteLine(await Exponent(10));
        }
    

    Then re-write Main:

        static void Main(string[] args)
        {
            Method();
            Console.ReadLine();
        }
    

    But, I don’t know the reasons behind why the code was originally written that way or even if there was a real purpose.

    Is it possible to use “return await (() => 2 << (n – 1));”

    It is not possible to write this line of code because nothing asynchronous is being performed. () => 2 << (n - 1) neither returns a Task object not has an GetAwaiter method associated with it; so, it can be executed asynchronously. thus, it can’t be “awaited”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently, I took it upon myself to try and learn OO programming. It has
this is my first question here so be gentle ! recently i try to
I recently found out about Cytoscape.js and would like to try out the force-directed
I've read about Single-Page Applications recently and wanted to try one out. After watching
I recently decided to try to learn some bash scripting and as a fun
I recently decided to try some java web development, so I installed xampp, the
I recently decided to try emacs for java development, so this is the first
I'm new to programming and thought Python would be a good language to learn.
I recently tried to learn about the defaulted and deleted functions in C++ 11
Recently I wanted to try some of the new features in Scala 2.9 in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.