本文介绍了如何在C#7中返回多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个队友告诉我,在C#7.0中,可以从一个函数本地返回多个值。有人可以提供一些例子吗?谢谢

A team mate told me that in C# 7.0 it's is possibile to return multiple values from a function natively. Can anybody provide some example? Thanks

推荐答案

本地是什么意思?

C#7具有一项新功能,借助元组类型元组文字,您可以从方法中返回多个值

C# 7 has a new feature that lets you return more than one value from a method thanks to tuple types and tuple literals.

以以下功能为例:

(string, string, string) MyCoolFunction() // tuple return type
{   
    //...        
    return (firstValue, secondValue, thirdValue);
}

可以这样使用:

var values = MyCoolFunction();
var firstValue = values.Item1;
var secondValue = values.Item2;
var thirdValue = values.Item3;

或使用解构语法

(string first, string second, string third) = MyCoolFunction();

//...

var (first, second, third) = MyCoolFunction(); //Implicitly Typed Variables

花点时间查看,一些非常好的例子(这个答案就是基于它们的!)。

Take some time to check out the Documentation, they have some very good examples (this answer's one are based on them!).

这篇关于如何在C#7中返回多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 07:55