ASP.NET函数返回多个值的几种方法

ASP.NET函数返回多个值的几种方法

一、使用 KeyValue 组合

  •  
  • C# 代码   复制
  • 
    static void Main(string[] args)
    {
        int int1 = 15;
        int int2 = 25;
        var result = Add_Multiply(int1, int2);
        Console.WriteLine(result.Key);
        Console.WriteLine(result.Value);
    }
    
    private static KeyValuePair<int, int> Add_Multiply(int int1, int int2)
    {
        var KeyValuePair = new KeyValuePair<int, int>(int1 + int2, int1 * int2);
        return KeyValuePair;
    }
    
    		
  • 二、使用 ref 参数

  •  
  • C# 代码   复制
  • 
    static void Main(string[] args)
    {
        int int1 = 15;
        int int2 = 25;
        int add = 0;
        int multiply = 0;
        Add_Multiply(int1, int2, ref add, ref multiply);
        Console.WriteLine(add);
        Console.WriteLine(multiply);
    }
    
    private static void Add_Multiply(int int1, int int2, ref int add, ref int multiply)
    {
        add = int1 + int2;
        multiply = int1 * int2;
    }
    
    		
  • 三、使用 out 参数

  •  
  • C# 代码   复制
  • 
    static void Main(string[] args)
    {
        int int1 = 15;
        int int2 = 25;
        int add = 0;
        int multiply = 0;
        Add_Multiply(int1, int2, out add, out multiply);
        Console.WriteLine(add);
        Console.WriteLine(multiply);
    }
    
    private static void Add_Multiply(int int1, int int2, out int add, out int multiply)
    {
        add = int1 + int2;
        multiply = int1 * int2;
    }
    
    		
  • 四、使用struct

  •  
  • C# 代码   复制
  • 
    struct Result
    {
        public int add;
        public int multiply;
    }
    static void Main(string[] args)
    {
        int int1 = 53;
        int int2 = 17;
        var result = Add_Multiply(int1, int2);
        Console.WriteLine(result.add);
        Console.WriteLine(result.multiply);
    }
    
    private static Result Add_Multiply(int int1, int int2)
    {
        var result = new Result
        {
            add = int1 + int2,
            multiply = int1 * int2
        };
        return result;
    }
    
    		
  • 五、使用class

  •  
  • C# 代码   复制
  • 
    class Result
    {
        public int add;
        public int multiply;
    }
    static void Main(string[] args)
    {
        int int1 = 13;
        int int2 = 27;
        var result = Add_Multiply(int1, int2);
        Console.WriteLine(result.add);
        Console.WriteLine(result.multiply);
    }
    
    private static Result Add_Multiply(int int1, int int2)
    {
        var result = new Result
        {
            add = int1 + int2,
            multiply = int1 * int2
        };
        return result;
    }
    
    		
  • 六、使用dynamic匿名类

  •  
  • C# 代码   复制
  • 
    static void Main(string[] args)
    {
        int int1 = 13;
        int int2 = 27;
        var result = Add_Multiply(int1, int2);
        Console.WriteLine(result.add);
        Console.WriteLine(result.multiply);
    }
    
    private static dynamic Add_Multiply(int int1, int int2)
    {
        var result = new
        {
            add = int1 + int2,
            multiply = int1 * int2
        };
        return result;
    }
    
    		
  • 七、使用 Tuple

  •  
  • C# 代码   复制
  • 
    static void Main(string[] args)
    {
        int int1 = 25;
        int int2 = 28;
        var result = Add_Multiply(int1, int2);
        Console.WriteLine(result.Item1);
        Console.WriteLine(result.Item2);
    }
    
    private static Tuple<int, int> Add_Multiply(int int1, int int2)
    {
        var tuple = new Tuple<int, int>(int1 + int2, int1 * int2);
        return tuple;
    }
    
    		
  • 标签: