I am implementing a simple lookup service using concurrent dictionary to store data.
Since most of the methods using this service will be async I am considering exposing the lookup functions with ValueTask<TResult>
.
Pseudocode:
public ValueTask<string> GetResultAsync(string key)
{
return ValueTask.FromResult(_dictionary.FirstOrDefault(p => p.Key == key).Value
}
Does this approach make sense and is there any caveats ?
Since most of the methods using this service will be async, I am considering exposing the lookup functions with
ValueTask
.
This is not a valid reason for exposing a fake-asynchronous wrapper of a synchronous operation. Doing so you are adding needless overhead, and you are communicating wrong/misleading semantics to the consumers of your service. In fact the only valid reason that I am aware of to do so, is if you are building a base class and you expect that the derived classes will override this method and provide real asynchronous implementations.