Monday, 1 June 2020

C# Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task'

 C# Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>'



Hello Friends,


In this article, I will explain how to solve Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>'


 C# Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>


In this week, I was working with one C# class where  I want to return a string from Task<string> but I was facing one error like "Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>'".

Please find below solution 


Solution

We need to use Task.FromResult to return string from C#.

Please find below example

public async Task<string> GetString()
{ 
   return await Task.FromResult("Hello");
}

Hope this helped you to solve your error
Happy Programming!!
Don’t forget to leave your feedback and comments below!
If you have any query mail me to Sujeet.bhujbal@gmail.com     
Happy programming!!
Don’t forget to leave your feedback and comments below!
Regards
Sujeet Bhujbal
--------------------------------------------------------------------------------
------------------------------------------------------------------------------


0 comments:

Post a comment