You are developing an application that uses multiple asynchronous tasks to optimize performance. You need to retrieve the result of an asynchronous task. Which code segment should you use?
A. Option A
B. Option B
C. Option C
D. Option D
5 Comments on “Which code segment should you use?”
“B” is the wrong answer because of ‘string result = GetData()’ – GetData not returned string so this could not be compiled.
“D” is the wrong answer ‘string result = async GetData()’ – ‘async’ could be used only in the method signature.
“A” and “C” answers are both correct. That fact that ‘C’ has ‘public async Task GetData()’ – only describes how this method will works (async or not). – so correct answer on this: ‘A and C’.
Same as below:
https://www.aiotestking.com/microsoft/which-code-segment-should-you-use-1082/
0
0
If you answer C, compiler rise a warning, but aslso suggest to remove async Task from GetData.
For me the right answer (that compiles as well) is A
1
1
should be C (https://msdn.microsoft.com/en-us/magazine/jj991977.aspx)
Async all the way Don’t mix blocking and async code
1
0
class AwnserC
{
protected async void StartTask()
{
string restult = await GetData();
}
public async Task GetData()
{
await Task.Delay(1000);
return “test”;
}
}
4
0
“B” is the wrong answer because of ‘string result = GetData()’ – GetData not returned string so this could not be compiled.
“D” is the wrong answer ‘string result = async GetData()’ – ‘async’ could be used only in the method signature.
“A” and “C” answers are both correct. That fact that ‘C’ has ‘public async Task GetData()’ – only describes how this method will works (async or not). – so correct answer on this: ‘A and C’.
2
1