是否有一种方法可以使用Tuple类,但在其中提供项目的名称?

例如:

public Tuple<int, int, int int> GetOrderRelatedIds()

它返回OrderGroupId、OrderTypeId、OrderSubTypeId和OrderRequirementId的id。

让我的方法的用户知道哪个是哪个就好了。(当您调用该方法时,结果为result。Item1,结果。第二条,结果。Item3 result.Item4。不清楚哪个是哪个。)

(我知道我可以创建一个类来保存所有这些id,但这些id已经有自己的类,它们生活在其中,为这个方法的返回值创建一个类似乎很愚蠢。)


当前回答

我想我会创建一个类,但另一种选择是输出参数。

public void GetOrderRelatedIds(out int OrderGroupId, out int OrderTypeId, out int OrderSubTypeId, out int OrderRequirementId)

因为你的元组只包含整数,你可以用Dictionary<string,int>来表示它

var orderIds = new Dictionary<string, int> {
    {"OrderGroupId", 1},
    {"OrderTypeId", 2},
    {"OrderSubTypeId", 3},
    {"OrderRequirementId", 4}.
};

但我也不建议这样做。

其他回答

在。net 4中,你也许可以看看ExpandoObject,但是,不要在这种简单的情况下使用它,因为编译时错误会变成运行时错误。

class Program
{
    static void Main(string[] args)
    {
        dynamic employee, manager;

        employee = new ExpandoObject();
        employee.Name = "John Smith";
        employee.Age = 33;

        manager = new ExpandoObject();
        manager.Name = "Allison Brown";
        manager.Age = 42;
        manager.TeamSize = 10;

        WritePerson(manager);
        WritePerson(employee);
    }
    private static void WritePerson(dynamic person)
    {
        Console.WriteLine("{0} is {1} years old.",
                          person.Name, person.Age);
        // The following statement causes an exception
        // if you pass the employee object.
        // Console.WriteLine("Manages {0} people", person.TeamSize);
    }
}
// This code example produces the following output:
// John Smith is 33 years old.
// Allison Brown is 42 years old.

另一个值得一提的是方法中的匿名类型,但是如果您想要返回它,则需要创建一个类。

var MyStuff = new
    {
        PropertyName1 = 10,
        PropertyName2 = "string data",
        PropertyName3 = new ComplexType()
    };

TL:DR ->系统。ValueTuples可以自定义字段System的名称。元组不能。

澄清一下,在c# 7.0及更高版本中有两种不同类型的元组。

系统。元组 而且 系统。ValueTuple

当你通过tuple <…>类:

public Tuple<int, string, int> GetUserInfo();

您正在声明一个Tuple对象数据类型。

当你通过圆括号声明一个元组时:

public (int id, string name, int age) GetUserInfo();

您正在声明ValueTuple值数据类型。

每一个都有不同的功能和行为。在你的问题中,你的方法返回一个系统。元组对象。

不幸的是,通过系统创建的元组对象。元组类没有内置功能来给每个属性一个自定义名称。它们总是默认为ItemN,这取决于它们包含多少属性。

系统。另一方面,ValueTuple值可以包含自定义命名字段。

要了解更多信息,您可以参考Tuple类型(c#参考)和/或上面每个类的链接。但从本质上讲,文档强调的两种不同类型元组的一些关键区别是:

c#元组,由System。ValueTuple类型是不同的 由System表示的元组。Tuple类型。主要的 区别如下: 系统。ValueTuple类型是值类型。系统。元组类型是引用类型。 系统。ValueTuple类型是可变的。系统。元组类型是不可变的。 系统的数据成员。ValueTuple类型是字段。系统的数据成员。元组类型是属性。

如果你的方法需要返回一个System。元组对象,或者你更想要这种类型的对象的行为,那么在写这篇文章时,你无法实现你想要的。但是,如果您的方法可以返回System。ValueTuple值,然后您可以在返回值中给它自定义命名字段。

为什么不使用多个返回而不是使用元组

var handler = GenerateFromMethod1(hits);
Process(handler.string1, handler.string1);

private static (string string1, string string2) GenerateFromMethod1()
{

}

在c# 7.0 (Visual Studio 2017)中,有一个新的结构可以做到这一点:

(string first, string middle, string last) LookupName(long id)

从这篇文章中复制我的答案,因为它更适合这里。

从c# v7.0开始,现在可以将元组属性命名为Item1、Item2等早先默认的名称。

命名元组字面量的属性:

var myDetails = (MyName: "Foo", MyAge: 22, MyFavoriteFood: "Bar");
Console.WriteLine($"Name - {myDetails.MyName}, Age - {myDetails.MyAge}, Passion - {myDetails.MyFavoriteFood}");

控制台的输出:

Name - Foo, Age - 22, Passion - Bar

从一个方法返回元组(有命名属性):

static void Main(string[] args)
{
    var empInfo = GetEmpInfo();
    Console.WriteLine($"Employee Details: {empInfo.firstName}, {empInfo.lastName}, {empInfo.computerName}, {empInfo.Salary}");
}

static (string firstName, string lastName, string computerName, int Salary) GetEmpInfo()
{
    //This is hardcoded just for the demonstration. Ideally this data might be coming from some DB or web service call
    return ("Foo", "Bar", "Foo-PC", 1000);
}

控制台的输出:

Employee Details: Foo, Bar, Foo-PC, 1000

创建具有命名属性的元组列表

var tupleList = new List<(int Index, string Name)>
{
    (1, "cow"),
    (5, "chickens"),
    (1, "airplane")
};

foreach (var tuple in tupleList)
    Console.WriteLine($"{tuple.Index} - {tuple.Name}");

控制台输出:

1 - cow  
5 - chickens  
1 - airplane

注意:本文中的代码片段使用了c# v6的字符串插值特性。