Tuples C#7.0中的元组文本能否支持面向方面编程

Tuples C#7.0中的元组文本能否支持面向方面编程,tuples,language-features,c#-7.0,valuetuple,Tuples,Language Features,C# 7.0,Valuetuple,我指的是元组文字,如下所述: 喜欢元组文字的想法 然而,我预见到会有很多查找返回元组中项的顺序的工作,我想知道我们如何解决这个问题 例如,将元组中的项的名称作为标识定义方面而不是顺序,这难道不是更有意义吗?或者有没有一种方法可以做到我不知道的 例如:假设NextEmployee()是一个库方法,我没有它的源代码,也没有很好的文档记录,假设它向我返回(名字:“jamie”,姓氏:“hince”,id:102348),我说: (string lastname, var _, int __) = Ne

我指的是元组文字,如下所述:

喜欢元组文字的想法

然而,我预见到会有很多查找返回元组中项的顺序的工作,我想知道我们如何解决这个问题

例如,将元组中的项的名称作为标识定义方面而不是顺序,这难道不是更有意义吗?或者有没有一种方法可以做到我不知道的

例如:假设NextEmployee()是一个库方法,我没有它的源代码,也没有很好的文档记录,假设它向我返回
(名字:“jamie”,姓氏:“hince”,id:102348)
,我说:

(string lastname, var _, int __) = NextEmployee(); // I only need the last name
编译器会很高兴地将firstname分配给lastname,或者对此发出警告或错误。为什么不把lastname映射到lastname呢


如果我们不必记住元组中lastname的索引,只需要像这样要求一个方面“lastname”,我会看到允许更松散耦合的体系结构。作为变量,您可以分配任何可分配给变量类型的值,而不考虑变量名称

与变量名一样,这些名称只是一种指示。返回值的唯一区别是编译器使用持久化元组元素的名称

事实上,即使存在名称,编译器也会警告您,如果您使用的名称与通常使用的名称不同,这是一个错误,并且语法仍然有效:

(string firstname, string lastname, int id) NextEmployee()
    => (apples: "jamie", potatos: "hince", oranges: 102348);
/*
Warning CS8123 The tuple element name 'apples' is ignored because a different name is specified by the target type '(string firstname, string lastname, int id)'.
Warning CS8123 The tuple element name 'potatos' is ignored because a different name is specified by the target type '(string firstname, string lastname, int id)'.
Warning CS8123 The tuple element name 'oranges' is ignored because a different name is specified by the target type '(string firstname, string lastname, int id)'.
*/
此处使用的语法为:

(string lastname, var _, int __) = NextEmployee();
不是元组声明语法,而是元组解构语法,用于创建
LastName
变量、
变量和
变量

这些都是产生相同结果的等价物:

  • (var lastname,var u,var u)=NextEmployee();//或
    var
    和类型名称的任意组合
  • var(lastname,uuuu)=NextEmployee()
要声明一个元组以接收方法的返回,您需要声明一个元组变量:

  • (stringfirstname,stringlastname,int-id)t=NextEmployee()
  • var t=NextEmployee()
但您的意图似乎是忽略
LastName
id
值:

(_, string lastname, _) = NextEmployee(); // declares a lastname variable and ignores firstname and id
但是如果您真的编写了
(string lastname,,)=NextEmployee()
,然后您将使用返回字符串“variable”
firstname
的值分配一个名为
lastname
的本地字符串变量


请记住,元组不是实体。它们是一组值。如果您使用的库使用元组作为实体,请注意,该库可能存在其他问题。

元组只是一包变量。作为变量,您可以分配任何可分配给变量类型的值,而不考虑变量名称

与变量名一样,这些名称只是一种指示。返回值的唯一区别是编译器使用持久化元组元素的名称

事实上,即使存在名称,编译器也会警告您,如果您使用的名称与通常使用的名称不同,这是一个错误,并且语法仍然有效:

(string firstname, string lastname, int id) NextEmployee()
    => (apples: "jamie", potatos: "hince", oranges: 102348);
/*
Warning CS8123 The tuple element name 'apples' is ignored because a different name is specified by the target type '(string firstname, string lastname, int id)'.
Warning CS8123 The tuple element name 'potatos' is ignored because a different name is specified by the target type '(string firstname, string lastname, int id)'.
Warning CS8123 The tuple element name 'oranges' is ignored because a different name is specified by the target type '(string firstname, string lastname, int id)'.
*/
此处使用的语法为:

(string lastname, var _, int __) = NextEmployee();
不是元组声明语法,而是元组解构语法,用于创建
LastName
变量、
变量和
变量

这些都是产生相同结果的等价物:

  • (var lastname,var u,var u)=NextEmployee();//或
    var
    和类型名称的任意组合
  • var(lastname,uuuu)=NextEmployee()
要声明一个元组以接收方法的返回,您需要声明一个元组变量:

  • (stringfirstname,stringlastname,int-id)t=NextEmployee()
  • var t=NextEmployee()
但您的意图似乎是忽略
LastName
id
值:

(_, string lastname, _) = NextEmployee(); // declares a lastname variable and ignores firstname and id
但是如果您真的编写了
(string lastname,,)=NextEmployee()
,然后您将使用返回字符串“variable”
firstname
的值分配一个名为
lastname
的本地字符串变量


请记住,元组不是实体。它们是一组值。如果您使用的库使用元组作为实体,请注意该库可能存在其他问题。

为什么不这样做?因为底层运行时甚至不知道名称

编译器必须在编译期间执行此操作。我们在哪里停?打字错误、大写字母等怎么办? 在我看来,目前的情况还可以

如果您对此问题有不同的看法,请在github的官方语言设计库中提出问题:


保罗已经很好地解释了技术细节,所以我不再重复了。

为什么不呢?因为底层运行时甚至不知道名称

编译器必须在编译期间执行此操作。我们在哪里停?打字错误、大写字母等怎么办? 在我看来,目前的情况还可以

如果您对此问题有不同的看法,请在github的官方语言设计库中提出问题:


保罗已经很好地解释了技术细节,所以我不再重复了。

不,小心!字段的顺序很重要,而不是名称。编译器将
(名字:“jamie”,姓氏:“hince”,id:102348)
转换为
(Item1:“jamie”,Item2:“hince”,Item3:102348)
。你不能通过名字来访问字段,实际上,所有的元组都只是一袋袋的变量。你所描述的是即将到来的记录