如何创建 ValueTuple 列表?
How to create a List of ValueTuple?
是否可以在 C# 7 中创建 ValueTuple 列表?
像这样:
List<(int example, string descrpt)> Method()
{
return Something;
}
此语法最适用于 c# 6
,但也可用于 c# 7
。其他答案更正确,因为他们倾向于使用 ValueTuple
而不是此处使用的 Tuple
。您可以看到 here 与 ValueTuple
的差异
List<Tuple<int, string>> Method()
{
return new List<Tuple<int, string>>
{
new Tuple<int, string>(2, "abc"),
new Tuple<int, string>(2, "abce"),
new Tuple<int, string>(2, "abcd"),
};
}
List<(int, string)> Method()
{
return new List<(int, string)>
{
(2, "abc"),
(2, "abce"),
(2, "abcd"),
};
}
当然,你可以这样做:
List<(int example, string descrpt)> Method() => new List<(int, string)> { (2, "x") };
var data = Method();
Console.WriteLine(data.First().example);
Console.WriteLine(data.First().descrpt);
您正在寻找这样的语法:
List<(int, string)> list = new List<(int, string)>();
list.Add((3, "first"));
list.Add((6, "second"));
你可以像你的情况那样使用:
List<(int, string)> Method() =>
new List<(int, string)>
{
(3, "first"),
(6, "second")
};
您也可以在返回前命名值:
List<(int Foo, string Bar)> Method() =>
...
您可以在(重新)命名它们时接收这些值:
List<(int MyInteger, string MyString)> result = Method();
var firstTuple = result.First();
int i = firstTuple.MyInteger;
string s = firstTuple.MyString;
只是添加到现有答案中,关于从现有枚举项中投影 ValueTuples
以及关于 属性 命名:
您仍然可以命名元组属性并仍然使用 var
类型推断(即不重复 属性 名称)通过在元组创建中提供属性的名称,即
var list = Enumerable.Range(0, 10)
.Select(i => (example: i, descrpt: $"{i}"))
.ToList();
// Access each item.example and item.descrpt
类似地,当从方法返回元组的枚举时,您可以在方法签名中命名属性,然后您不需要在方法内部再次命名它们:
public IList<(int example, string descrpt)> ReturnTuples()
{
return Enumerable.Range(0, 10)
.Select(i => (i, $"{i}"))
.ToList();
}
var list = ReturnTuples();
// Again, access each item.example and item.descrpt
是否可以在 C# 7 中创建 ValueTuple 列表?
像这样:
List<(int example, string descrpt)> Method()
{
return Something;
}
此语法最适用于 c# 6
,但也可用于 c# 7
。其他答案更正确,因为他们倾向于使用 ValueTuple
而不是此处使用的 Tuple
。您可以看到 here 与 ValueTuple
List<Tuple<int, string>> Method()
{
return new List<Tuple<int, string>>
{
new Tuple<int, string>(2, "abc"),
new Tuple<int, string>(2, "abce"),
new Tuple<int, string>(2, "abcd"),
};
}
List<(int, string)> Method()
{
return new List<(int, string)>
{
(2, "abc"),
(2, "abce"),
(2, "abcd"),
};
}
当然,你可以这样做:
List<(int example, string descrpt)> Method() => new List<(int, string)> { (2, "x") };
var data = Method();
Console.WriteLine(data.First().example);
Console.WriteLine(data.First().descrpt);
您正在寻找这样的语法:
List<(int, string)> list = new List<(int, string)>();
list.Add((3, "first"));
list.Add((6, "second"));
你可以像你的情况那样使用:
List<(int, string)> Method() =>
new List<(int, string)>
{
(3, "first"),
(6, "second")
};
您也可以在返回前命名值:
List<(int Foo, string Bar)> Method() =>
...
您可以在(重新)命名它们时接收这些值:
List<(int MyInteger, string MyString)> result = Method();
var firstTuple = result.First();
int i = firstTuple.MyInteger;
string s = firstTuple.MyString;
只是添加到现有答案中,关于从现有枚举项中投影 ValueTuples
以及关于 属性 命名:
您仍然可以命名元组属性并仍然使用 var
类型推断(即不重复 属性 名称)通过在元组创建中提供属性的名称,即
var list = Enumerable.Range(0, 10)
.Select(i => (example: i, descrpt: $"{i}"))
.ToList();
// Access each item.example and item.descrpt
类似地,当从方法返回元组的枚举时,您可以在方法签名中命名属性,然后您不需要在方法内部再次命名它们:
public IList<(int example, string descrpt)> ReturnTuples()
{
return Enumerable.Range(0, 10)
.Select(i => (i, $"{i}"))
.ToList();
}
var list = ReturnTuples();
// Again, access each item.example and item.descrpt