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
Sample questions paper for Psychometric tests
Sample questions…
Psychometric tests for MBAs The word…
Behavioral interview questions and Sample Answers
Behavioral interview…
The quickest way to get the hiring manager…
Test For Financial Analyst
Test For Financial…
Financial analysts are charged with the…
Aptitude examples
Aptitude examples
In the numerical aptitude test question…
Personality Development tests online
Personality Development…
Business psychologists have kindly helped…
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.