在C#中,您可以使用名称定义值元组的别名吗?

时间:2017-04-03 09:30:56

标签: c# c#-7.0

我知道可以使用使用关键字在C#中定义别名。

e.g。

using ResponseKey = System.ValueTuple<System.Guid, string, string>;

但是,是否可以使用值元组的新语法定义一个?

using ResponseKey = (Guid venueId, string contentId, string answer);

此语法似乎不起作用。应该吗?

3 个答案:

答案 0 :(得分:47)

This has been requested, and recorded in the Roslyn repo on Github。然而,它在那里收到了混合的接收,而the proposed record types,将超过这个要求。

问题在Roslyn回购but is now being tracked in the C# language repo中已经关闭。

答案 1 :(得分:1)

using定义为: using identifier = namespace-or-type-name;MSDN)。

(Guid venueId, string contentId, string answer)既不是命名空间也不是(完全限定的)类型名称。

但我大多猜测。甚至不知道C#7.0直到现在才存在。

答案 2 :(得分:0)

您可以扩展System.Tuple-类似以下内容:

x match {
  case _ if x > 0 && x <= 10 => 0
  case _ if x <= 20          => 1
  case _ if x <= 30          => 2
}
相关问题