Efficient use of the C#tuple to return multiple values
In the programming language, returning multiple values from the method is a common problem, and C#is no exception. Different from the language that needs to be used or referenced, C#provides a simpler way to use the cost group to achieve this purpose.
C# 7 in the Yuan Group
With the introduction of C# 7, Yuan composition has a language characteristics that represents a collection of different types of values. This allows the creation to contain multiple returns that can contain multiple values.For example, consider a method of searching for names, middle names and surnames based on a given ID. In C# 7, you can define the return type as the meta group:
(string first, string middle, string last) LookupName(long id)
Then, the method can retrieve the value and return it as the meta -group:
<> The return of the returned Yuan group
return (first, middle, last);
Code can use the Item property to access the return value:
<命> Naming element elements
var names = LookupName(id); WriteLine($"found {names.Item1} {names.Item3}.");
C#also provides the function of naming the elements to improve the definition. This can be completed by adding a name to the method signature or Return statement:
or <或者><构> deconstruction
(string first, string middle, string last) LookupName(long id)
C# 7 also supports deconstruction. This allows you to easily declare variables with the same name as the element of the tuple and assign a value in a single line:
return (first: first, middle: middle, last: last);
For more examples and details, please refer to the link provided. (The original map should be kept here)
The above is the detailed content of How Can C# Methods Return Multiple Values Efficiently Using Tuples?. For more information, please follow other related articles on the PHP Chinese website!