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

例如:

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

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

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

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


当前回答

c# 7元组的例子

var tuple = TupleExample(key, value);

     private (string key1, long value1) ValidateAPIKeyOwnerId(string key, string value)
            {
                return (key, value);
            }
      if (!string.IsNullOrEmpty(tuple.key1) && tuple.value1 > 0)
          {
                    //your code

                }     

其他回答

您可以编写一个包含元组的类。

您需要重写Equals和GetHashCode函数

和==和!=操作符。

class Program
{
    public class MyTuple
    {
        private Tuple<int, int> t;

        public MyTuple(int a, int b)
        {
            t = new Tuple<int, int>(a, b);
        }

        public int A
        {
            get
            {
                return t.Item1;
            }
        }

        public int B
        {
            get
            {
                return t.Item2;
            }
        }

        public override bool Equals(object obj)
        {
            return t.Equals(((MyTuple)obj).t);
        }

        public override int GetHashCode()
        {
            return t.GetHashCode();
        }

        public static bool operator ==(MyTuple m1, MyTuple m2)
        {
            return m1.Equals(m2);
        }

        public static bool operator !=(MyTuple m1, MyTuple m2)
        {
            return !m1.Equals(m2);
        }
    }

    static void Main(string[] args)
    {
        var v1 = new MyTuple(1, 2);
        var v2 = new MyTuple(1, 2);

        Console.WriteLine(v1 == v2);

        Dictionary<MyTuple, int> d = new Dictionary<MyTuple, int>();
        d.Add(v1, 1);

        Console.WriteLine(d.ContainsKey(v2));
    }
}

将返回:

True

True

在。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()
    };

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

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}.
};

但我也不建议这样做。

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值,然后您可以在返回值中给它自定义命名字段。

不,你不能命名元组成员。

中间的方法是使用ExpandoObject而不是Tuple。