C# 多参数返回 从基本到7.0

原文地址: http://www.cnblogs.com/ylvict/p/5573094.html

•使用 KeyValue 组合

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;
}
keyValue

 

•使用 ref/out 参数

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;
}
Ref
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;
}
Out

 

•使用 struct 或者 class 

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;
}
Struct
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;
}
Class

 

•dynamic

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;
}
Dynamic

 

•使用 Tuple

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;
}
Tuple

 

新特性(C#7.0)

static void Main(string[] args)
{
 int int1 = 25;
 int int2 = 28;
 var result = Add_Multiply(int1, int2);
 Console.WriteLine($"Add: {result.add}, Multiply: {result.multiply}");
 //(var add, var multiply) = Add_Multiply(int1, int2);
 //Console.WriteLine($"Add: {add}, Multiply: {multiply}");
}
public (int add, int multiply) Add_Multiply(int int1, int int2) 
 => (int1 + int2, int1 * int2);
C#7.0语法

 

转载于:https://www.cnblogs.com/Bruke/p/8880640.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值