在 C# 中,您可以为具有名称的值元组定义别名吗?
In C# can you define an alias to a value tuple with names?
我知道可以使用 using 关键字在 C# 中定义别名。
例如
using ResponseKey = System.ValueTuple<System.Guid, string, string>;
但是,是否可以使用值元组的新语法定义一个?
using ResponseKey = (Guid venueId, string contentId, string answer);
此语法似乎不起作用。应该吗?
已更新。从 C# 10 开始,您可以定义结构或基于 class 的记录来满足此要求:
public record struct ResponseKey(Guid venueId, string contentId, string answer);
public record class ResponseKey(Guid venueId, string contentId, string answer);
请注意,对于第二个定义,class
是可选的。
This has been requested, and recorded in the Roslyn repo on Github. However it received a mixed reception there, and the proposed record types,将超过此要求。
问题已在 Roslyn 存储库中关闭 but is now being tracked in the C# language repo。
我知道可以使用 using 关键字在 C# 中定义别名。
例如
using ResponseKey = System.ValueTuple<System.Guid, string, string>;
但是,是否可以使用值元组的新语法定义一个?
using ResponseKey = (Guid venueId, string contentId, string answer);
此语法似乎不起作用。应该吗?
已更新。从 C# 10 开始,您可以定义结构或基于 class 的记录来满足此要求:
public record struct ResponseKey(Guid venueId, string contentId, string answer);
public record class ResponseKey(Guid venueId, string contentId, string answer);
请注意,对于第二个定义,class
是可选的。
This has been requested, and recorded in the Roslyn repo on Github. However it received a mixed reception there, and the proposed record types,将超过此要求。
问题已在 Roslyn 存储库中关闭 but is now being tracked in the C# language repo。