📜  C#|用元组解构

📅  最后修改于: 2021-05-29 14:30:25             🧑  作者: Mango

元组是一种数据结构,它为您提供了一种表示具有多个可能(可能不相关)的值的数据集的最简单方法。但是,如果您尝试从元组中检索多个字段或属性值,则更加困难。因此,为克服此问题,在C#7.0中引入了析构函数。它用于将变量值或元组划分为多个部分,并将这些值分配给新变量。它也与类和结构一起使用。在这里,我们仅讨论使用元组进行解构的工作。

在元组中,使用解构函数将元组划分为多个部分,并将这些部分分别分配给新变量。因此,您可以访问单个字段或属性值。您可以通过四种不同的方式来构造一个元组:

1.您可以通过在括号内显式声明每个字段的类型来解构元组。但是,即使元组中的每个字段都是同一类型,也不允许在括号外指定特定类型。如果尝试这样做,则会出现错误。

例子:

// C# program to illustrate the concept 
// of deconstruction with the tuple
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace ConsoleApp1 {
  
class GFG {
  
    // This method returns the Pet details
    public(string, string, int, int, string) PetDetails(string type, 
                     string name, int height, int age, string color)
    {
        string p_type = type;
        string p_name = name;
        int p_height = height;
        int p_age = age;
        string p_color = color;
        return (p_type, p_name, p_height, p_height, p_color);
    }
  
    // Main method
    static void Main(string[] args)
    {
  
        // Creating object of GFG class
        GFG g = new GFG();
  
        // Deconstruct the given tuple
        // So that we can directly access individual fields
        // By explicitly declaring types
        (string type, string name, int height, int age, string color) = g.PetDetails("Dog", 
                                                                "Dollar", 124, 3, "White");
        Console.WriteLine("Pet Details:");
        Console.WriteLine("Type: " + type);
        Console.WriteLine("Name: " + name);
        Console.WriteLine("Height: " + height);
        Console.WriteLine("Age: " + age);
        Console.WriteLine("Color: " + color);
        Console.ReadLine();
    }
}
}

输出:

2.您可以使用var关键字解构元组,以便C#推断每个变量的类型。您可以通过两种不同的方式使用var关键字:

  • 您可以将var关键字放在括号之外。
  • 您可以将var关键字分别放在带有某些或所有变量的括号内。

例子:

// C# program to illustrate the concept 
// of deconstruction with the tuple
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace ConsoleApp1 {
  
class GFG {
  
    // This method returns the Pet details
    public(string, string, int, int, string) PetDetails(string type, 
                     string name, int height, int age, string color)
    {
        string p_type = type;
        string p_name = name;
        int p_height = height;
        int p_age = age;
        string p_color = color;
        return (p_type, p_name, p_height, p_height, p_color);
    }
  
    // Main method
    static void Main(string[] args)
    {
  
        // Creating object of GFG class
        GFG g = new GFG();
  
        // Deconstruct the given tuple
        // So that we can directly 
        // access individual fields
        // Using var keyword
        var(type1, name1, height1, age1, color1) = g.PetDetails("Dog",
                                           "Dollar", 124, 3, "White");
        Console.WriteLine("Pet Details:");
        Console.WriteLine("Type: " + type1);
        Console.WriteLine("Name: " + name1);
        Console.WriteLine("Height: " + height1);
        Console.WriteLine("Age: " + age1);
        Console.WriteLine("Color: " + color1);
  
        (var type2, var name2, var height2, var age2, var color2) = g.PetDetails("Cat", 
                                                         "Poo", 104, 1, "Black&White");
        Console.WriteLine("\nPet Details:");
        Console.WriteLine("Type: " + type2);
        Console.WriteLine("Name: " + name2);
        Console.WriteLine("Height: " + height2);
        Console.WriteLine("Age: " + age2);
        Console.WriteLine("Color: " + color2);
        Console.ReadLine();
    }
}
}

输出:

3.您可以将元组解构为已声明的变量。如下例所示:

例子:

// C# program to illustrate the concept 
// of deconstruction with the tuple
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace ConsoleApp1 {
  
class GFG {
  
    // This method returns the Pet details
    public(string, string, int, int, string) PetDetails(string type, 
                     string name, int height, int age, string color)
    {
        string p_type = type;
        string p_name = name;
        int p_height = height;
        int p_age = age;
        string p_color = color;
        return (p_type, p_name, p_height, p_height, p_color);
    }
  
    // Main method
    static void Main(string[] args)
    {
  
        // Creating object of GFG class
        GFG g = new GFG();
  
        // Declaring and initializing variables
        string type = "Cow";
        string name = "BooBoo";
        int height = 234;
        int age = 4;
        string color = "Black&white";
  
        // Deconstruct the given tuple
        // So that we can directly 
        // access individual fields
        // By declaring variables
        (type, name, height, age, color) = g.PetDetails("Cat", 
                                      "Mew", 105, 2, "Brown");
        Console.WriteLine("Pet Details:");
        Console.WriteLine("Type: " + type);
        Console.WriteLine("Name: " + name);
        Console.WriteLine("Height: " + height);
        Console.WriteLine("Age: " + age);
        Console.WriteLine("Color: " + color);
        Console.ReadLine();
    }
}
}

输出:

4.您也可以在解构中使用丢弃物。丢弃是只写变量,其值应被忽略。并且在分配中使用下划线字符(“ _”)来指定丢弃。您可以根据需要舍弃任意多个值,并且所有值都由单个舍弃_表示。

句法:

例子:

// C# program to illustrate the concept
// of deconstruction with tuple
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace ConsoleApp1 {
  
class GFG {
  
    // This method returns 
    // the Pet details
    public(string, string, int, int, string) PetDetails(string type,
                     string name, int height, int age, string color)
    {
        string p_type = type;
        string p_name = name;
        int p_height = height;
        int p_age = age;
        string p_color = color;
        return (p_type, p_name, p_height, p_height, p_color);
    }
  
    // Main method
    static void Main(string[] args)
    {
  
        // Creating object of GFG class
        GFG g = new GFG();
  
        // Discarding field values
        // in deconstruction
  
        (string type, _, int height, _, string color) = g.PetDetails("Dog",
                                                "Dollar", 124, 3, "White");
        Console.WriteLine("Pet Details:");
        Console.WriteLine("Type: " + type);
        Console.WriteLine("Height: " + height);
        Console.WriteLine("Color: " + color);
        Console.ReadLine();
    }
}
}

输出:

注意:在解构中,如果要消除任何元素,则需要将每个元素分配给一个变量,然后编译器将给出错误信息。而且,如果您消除了任何元素,则不允许在解构的左侧混合对现有变量的声明和赋值,否则编译器将给出错误消息。