c#7.x使用元组类型的短名称

Shy*_*Lee 5 c# tuples

通过使用元组,我可以通过替换很多结构来大规模简化我的代码.除了这里有一个捕获物阻止我这样做.

如何在创建"使用"快捷方式时保留元组中每个项目的名称?我已经在下面的代码中用我的评论创建了示例.

/* This way is desired because it keeps the naming for each item in the Tuple, 
but incorrect grammar for C# 7.3 */
using Employee = (string Name, DateTime StartTime, DateTime LeaveTime, 
int Id, double Payment);

/* This way is the correct grammar for C# 7.3, but I lost all the names for the items, 
and I have to use Item1, Item2 and so on.... */
using Employee = Tuple<string, DateTime, DateTime, int, double>;

public class TestFunc
{
    public Employee GetEmployee()
    {
        Employee value; // I know it is an errorous usage here.
        value.Name = "Steve"; // Just want to make an example.
        value.StartTime = new DateTime();
        value.EndTime = new DateTime();
        return value;
    }
}
Run Code Online (Sandbox Code Playgroud)

Dam*_*ver 3

没有更多的打字:

struct Employee {public string Name; public DateTime StartTime; public DateTime LeaveTime; 
public int Id; public double Payment}
Run Code Online (Sandbox Code Playgroud)

抛开关于可变structs 1的邪恶的通常讨论;一旦您采取了这一步,就可以采取很多措施来改善这一情况。但这是几乎满足您要求的最小方法(除了它确实具有这些命名字段而不是编译器欺骗)。

您尚未展示它的所有用法,因此您可能还想实现其他构造函数和Deconstruct方法。


1因为如果您已经在使用ValueTuples,那么您就已经在使用可变structs,并且希望已经对它们保持警惕。