In order to solve the error

Programming Interview questions C#


The answer to the first part of the question (i.e., the version of the code with await Task.Delay(5);) is that the program will just output a blank line (not “Hello world!”). This is because result will still be uninitialized when Console.WriteLine is called.

Most procedural and object-oriented programmers expect a function to execute from beginning to end, or to a return statement, before returning to the calling function. This is not the case with C# async functions. They only execute up until the first await statement, then return to the caller. The function called by await (in this case Task.Delay) is executed asynchronously, and the line after the await statement isn’t signaled to execute until Task.Delay completes (in 5 milliseconds). However, within that time, control has already returned to the caller, which executes the Console.WriteLine statement on a string that hasn’t yet been initialized.

Calling await Task.Delay(5) lets the current thread continue what it is doing, and if it’s done (pending any awaits), returns it to the thread pool. This is the primary benefit of the async/await mechanism. It allows the CLR to service more requests with less threads in the thread pool.

Asynchronous programming has become a lot more common, with the prevalence of devices which perform over-the-network service requests or database requests for many activities. C# has some excellent programming constructs which greatly ease the task of programming asynchronous methods, and a programmer who is aware of them will produce better programs.

With regard to the second part of the question, if await Task.Delay(5); was replaced with Thread.Sleep(5), the program would output Hello world!. An async method without at least one await statement in it operates just like a synchronous method; that is, it will execute from beginning to end, or until it encounters a return statement. Calling Thread.Sleep simply blocks the currently running thread, so the Thread.Sleep(5) call just adds 5 milliseconds to the execution time of the SaySomething method.



Share this article





Related Posts



Latest Posts
Flight nurse interview questions
Flight nurse…
—A Paramedic, Nurse and Pilot Survival…
Top 10 phone interview questions and Answers
Top 10 phone…
Photo: Rustie views new construction…
Computer test for job interview
Computer test…
A c omputer basic skills test is usually…
Company personality tests
Company personality…
A decade ago, researchers discovered…
Graphic design job interview questions
Graphic design…
About a month and half ago, my friend…
Search
Featured posts
  • Java Object Oriented programming Interview questions
  • JavaScript programming Interview questions
  • Multithreaded programming Interview questions
  • PL SQL programming Interview questions and answers
  • Program Manager Interview questions
  • Games Programmer Interview questions
  • Programming technical interview questions
  • SAP Basis technical interview questions
  • Top 10 manager interview questions
Copyright © 2024 l www.floydfairnessfund.org. All rights reserved.