Home >Backend Development >C++ >How Can I Return Multiple Values from a C# Method?
The multiple values in the c# return
Unlike version of C, C# supports multiple values from method calls. The following is the method of achieving this goal:
Use the Yuan Group (C# 7 and higher versions)
C# 7 The meta -group introduced in the c# 7 allows you to bundle multiple values into a single entity. You can declare a method to return the type group type, for example:
Then you can retrieve these values in the following way:
<code class="language-csharp">(string, string, string) LookupName(long id) // 元组返回类型 { // 从数据存储中检索名、中间名和姓 return (first, middle, last); // 元组字面量 }</code>
Naming element elements
<code class="language-csharp">var names = LookupName(id); Console.WriteLine($"找到 {names.Item1} {names.Item3}。");</code>
You can also name the element of the tuple to avoid using "item1" and "item2". This can be completed by adding the name to the method of signing or returning the value of the method:
deconstruction
<code class="language-csharp">(string first, string middle, string last) LookupName(long id) // 命名元组元素 { return (first: first, middle: middle, last: last); // 字面量中的命名元组元素 }</code>Discretion is another convenient function of the Yuan group. It allows you to decompose the meta -group into a single variable:
In addition, you can find more examples and details on the official documentation website of Microsoft, and explore other resources provided to obtain further explanations.
The above is the detailed content of How Can I Return Multiple Values from a C# Method?. For more information, please follow other related articles on the PHP Chinese website!