C#方法的多值返回方式

  1. 定义一个class或者struct接收方法的返回值。
  2. 使用out编辑方法参数。
  3. 直接定义不同类型的返回值。
  4. 使用元组(Tuple - 引用元组,ValueTuple - 值元组)接收方法的返回值。

下面代码演示了后两种的使用:

 1 using System;
 2 
 3 namespace a
 4 {
 5     public class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             var tupple = GetValue();
10             //Tuple<string, int, float> tupple = GetValue();
11             Console.WriteLine(tupple.Item2);
12 
13             //不关注的返回值用 _ 接受
14             var (_, _, height) = GetVal();
15             Console.WriteLine(height);
16 
17             Console.ReadKey();
18         }
19 
20         static Tuple<string, int, float> GetValue()
21         {
22             return Tuple.Create("九戒", 20, 185.2f);
23         }
24 
25         static (string name, int age, float height) GetVal()
26         {
27             return ("九戒", 20, 185.2f);
28         }
29     }
30 }
原文地址:https://www.cnblogs.com/luguoshuai/p/15002880.html