Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I declare named async tuple functions in C#?

C# 7 allows us to declare functions that return named tuples, so a declaration like the one illustrated below is now ok.

(int quotient, int remainder) = GetDivisionResults(17, 5);

I have need to process the response from an HTTP Client request and extract two values from it, and I want to do so asynchronously. The following is allowed

public static async Task<<Tuple<string,string>> GetRequiredReturnValuesFromResponse(string response)

My simple question is, is it possible to name the output strings in the same way that I could were the function to operate synchronously along the lines as illustrated below?

public static async Task<Tuple< string OrderNumber,string OriginalId>> GetRequiredReturnValuesFromResponse(string response)
like image 934
Dom Sinclair Avatar asked May 25 '18 06:05

Dom Sinclair


People also ask

Can you name tuples C#?

Starting C# v7. 0, it is now possible to name the tuple properties which earlier used to default to names like Item1 , Item2 and so on.

Can async methods declare out parameters?

The only way to have supported out-by-reference parameters would be if the async feature were done by a low-level CLR rewrite instead of a compiler-rewrite.

What is an async function C#?

An async method runs synchronously until it reaches its first await expression, at which point the method is suspended until the awaited task is complete. In the meantime, control returns to the caller of the method, as the example in the next section shows.

Can async function return string?

The async method returning Task in C# It means the async method either does not have a return statement in it or it may contain a return statement that doesn't return any value. Such type of async methods returns void if they run synchronously.


1 Answers

You can't name Tuple elements - only ValueTuple elements. C# 7 tuple support is based on the ValueTuple type. But even then, the names aren't part of ValueTuple itself... they're stored in attributes alongside the return type (or parameter type).

So you can definitely declare this:

public static async Task<(string OrderNumber, string OriginalId)>
    GetRequiredReturnValuesFromResponse(string response)

You can then use:

var tuple = await GetRequiredReturnValuesFromResponse(response);
Console.WriteLine(tuple.OrderNumber);
Console.WriteLine(tuple.OriginalId);

Or even:

var (number, id) = await GetRequiredReturnValuesFromResponse(response);

But you can't name the elements when explicitly using ValueTuple (unless you specify the naming attributes explicitly, which I believe the compiler warns you against anyway) or using Tuple at all.

Personally I'd recommend against putting ValueTuple types in public APIs; I typically regard them as a stop-gap measure for an internal implementation which I might decide to migrate to a more fully-encapsulated type later on. But that's just a personal opinion, and doesn't represent what you can do.

like image 199
Jon Skeet Avatar answered Sep 29 '22 02:09

Jon Skeet