C# 元组 ValueTuple

C# 7.0 (.NET Framework 4.7) 引入了 ValueTuple 结构,它是元组的值类型表示。

ValueTuple 仅在 .NET Framework 4.7 中可用。 如果您在项目中没有看到 ValueTuple,那么您需要安装 ValueTuple。 (.NET Framework 4.7 或更高版本,或者 .NET Standard Library 2.0 或更高版本已经包含 ValueTuple。)

要安装 ValueTuple 包,请右键单击解决方案资源管理器中的项目并选择管理 NuGet 程序包... 这将打开 NuGet 包管理器。 单击浏览选项卡,在搜索框中搜索ValueTuple,选择System.ValueTuple包,如下图。

ValueTuple 跟 Python

元组(ValueTuple)初始化

创建和初始化 ValueTuple 很容易。 可以使用括号 () 并指定其中的值来创建和初始化它。

var person = (1, "Malema", ".net");
//等价于下面这种.
var person = ValueTuple.Create(1, "Malema", ".net"); //注意不是 Tuple.Create

元组至少需要两个值。 以下第一个示例不是元组。

var number = (1);  // int 类型 不是一个元组
var numbers = (1, 2); // 合法的元组

跟 Tuple不一样 ValueTuple可以包含8个以上的元素

var numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);

指定名称

我们可以为 ValueTuple 属性指定名称,而不是使用默认的属性名称,如 Item1、Item2 等。

static void Main(string[] args)
{
    //(int id, string firstName, string lastName) = (1, "Malema", ".net");  //这是可以的
    //(var id, var firstName, var lastName) = (1, "Malema", ".net");  //这也行
    var (id, firstName, lastName) = (1, "Malema", ".net"); 
    Console.WriteLine(id); // 1
    Console.WriteLine(firstName); // Malema
    Console.WriteLine(lastName); // .net
}

我们还可以为右侧的成员名称分配值,如下所示。

static void Main(string[] args)
{
    var person = (Id: 1, FirstName: "Malema", LastName: ".net");
    Console.WriteLine(person.Id); // 1
    Console.WriteLine(person.FirstName); // Malema
    Console.WriteLine(person.LastName); // .net
}

请注意,我们可以在左侧或右侧提供成员名称,但不能在两侧提供。 左侧优先于右侧。 以下将忽略右侧的名称。 IDE 也会有警告的

 (int id, string firstName, string lastName) = (Id: 1, FirstName: "Malema", LastName: ".net");

我们也可以把变量赋值给元素的成员

string firstName = "hello", lastName = "world";
var person = (FirstName: firstName, LastName: lastName);

ValueTuple 元组做为参数传递

static void Main(string[] args)
{
    DisplayTuple((1, "Bill", "Gates"));
}

static void DisplayTuple((int id, string firstName, string lastName) person)
{
    Console.WriteLine($"Id = {person.id}"); // Id = 1
    Console.WriteLine($"First Name = { person.firstName}"); // First Name = Bill
    Console.WriteLine($"Last Name = { person.lastName}"); // Last Name = Gates
}

ValueTuple 元组做为返回值

static void Main(string[] args)
{
    var person = GetPerson();
}

//在这边定义上成员名字。这样调用的地方也可以很方便的使用,打点的话会有智能提示
static (int Id, string FirstName, string LastName) GetPerson()
{
    return (1, "Bill", "Gates");
}

解构 Deconstruction

我们可以通过解构,直接取出元组中的成员。 上面的代码可以变成如下的形式

static void Main(string[] args)
{
    (int id, string firstName, string lastName) = GetPerson();
}

static (int Id, string FirstName, string LastName) GetPerson()
{
    return (1, "Bill", "Gates");
}

当我们不需要某个成员的时候我们可以使用 _ 称为弃元discards。

class Program
{
    static void Main(string[] args)
    {
        (int id, string firstName, _) = GetPerson();
    }

    static (int Id, string FirstName, string LastName) GetPerson()
    {
        return (1, "Bill", "Gates");
    }
}

类也是可以进行解构的。如下

class Program
{
    static void Main(string[] args)
    {

        (int id, string firstName, string lastName) = GetPerson();
    }

    static Person GetPerson()
    {
        return new Person();
    }


    class Person
    {
        public void Deconstruct(out int id, out string firstName, out string lastName)
        {
            id = 3;
            firstName = "malema";
            lastName = ".net";
        }
    }
}
上一篇:C# 元组Tuple
最近更新的
...