我有一些类型S
和T
(例如S=object
和T=string
)。我有一对这些类型的数组,即
(S[], T[]) pairOfArrays;
Run Code Online (Sandbox Code Playgroud)
我想将其转换为成对的数组,即
(S, T)[] arrayOfPairs;
Run Code Online (Sandbox Code Playgroud)
我怎么做?如果可能的话,我对 LINQ 解决方案最感兴趣。请假设 中的两个数组pairOfArrays
具有相同的长度。
我知道另一个方向:
pairOfArrays = (arrayOfPairs.Select(i => i.Item1).ToArray(),
arrayOfPairs.Select(i => i.Item2).ToArray());
Run Code Online (Sandbox Code Playgroud)
我所寻求的方向的问题是:为了迭代(使用Select
等)我需要一个IEnumerable
,但(S[], T[])
不是IEnumerable
。如果我从 开始,那么我不知道如何获取与当前语句中pairOfArrays.Item1.Select(...
相同的条目(按索引)。Item2
Select
我刚刚将 DLL 文件包含到我的项目中,并使用返回List<Tuple>
. 我最近才意识到ValueTuple
C# 7 中的数据类型,并想使用它来代替返回的普通元组。我知道我可以循环并手动进行转换,但是我想尽可能避免这样做,并且想知道是否有人知道将 Tuple 转换为 ValueTuple 的简写方法?如果我无知并且这是不可能的,那么如果有人能让我意识到实现这种转换的最有效方法,我将不胜感激。
我寻找了一种.ToValueTuple()
方法,并在返回列表的函数上使用了基本的转换,但没有任何乐趣。
尝试时:
List<(string entityName, int min, int average, int max)> tupTest = trs.GetEntityStats();
Run Code Online (Sandbox Code Playgroud)
我收到错误:
无法将源类型“System.Collections.Generic.List>”转换为目标类型“System.Collections.Generic.List<(string entityName, int min, intaverage, int max)>”
我在StackOverflow中搜索,没有找到任何文章或与之相关的任何内容。
例如,下面的示例描述了ValueTuple数组
(string CategoryName, params string[] Properties)[] MyArrayValueTupleParameter // Compile-Time Syntax error
Run Code Online (Sandbox Code Playgroud)
请注意,前面的示例用作参数。不是一个变量。
但只有string[]
在没有参数的情况下才有效?我在这里错过了什么或者默认情况下不支持它吗?
乍看上去:
这有效
void ShowAppearanceCategories((string CategoryName, string[] Properties)[] VisibleCategories)
{
foreach (var Row in PropertyGridControl.Rows)
{
var VisibleCategory = VisibleCategories.FirstOrDefault(x => x.CategoryName == Row.Name);
if (VisibleCategory != default)
{
foreach (var ChildRow in Row.ChildRows)
{
if (VisibleCategory.Properties.Any(x => ChildRow.Name.Contains(x)))
{
ChildRow.Visible = false;
}
}
}
else
{
Row.Visible = false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这不起作用
void ShowAppearanceCategories((string CategoryName, params string[] Properties)[] …
Run Code Online (Sandbox Code Playgroud) C# 7.3 中引入了值元组类型之间的相等性。它允许这样的代码:
var x = (1, 2);
var y = (1, 2);
if(x == y) ...
Run Code Online (Sandbox Code Playgroud)
这工作正常并给出正确的结果。然而,编译器引入了元组对象的隐藏副本,并比较它们,给出等价的:
var V_3 = x;
var V_4 = y;
if(V_3.Item1 == V_4.Item1 && V_3.Item2 == V_4.Item2) ...
Run Code Online (Sandbox Code Playgroud)
是V_3
和V_4
刚才的防守副本?如果是这样,他们在防御什么?
这是我能想到的最小的例子,但我已经尝试过使用用户定义的结构和方法返回/属性作为元组成员以及类似(并不总是相同)的输出。
我正在使用 .Net SDK 的 v5.0.202 和 C# v9,但从 .Net Core 3 开始就看到了这种行为。
我创建了一个方法:
\npublic static Tuple<string, string, string> SplitStr(string req)\n{\n //...\n return (A, B, C)\n}\n
Run Code Online (Sandbox Code Playgroud)\n它抱怨说"CSOOZQ: Cannot implicitly convert type '(strinq _keyword, strinq _filterCondition, strinq _filterCateqory)' to 'System.Tuple<strinq, strinq, string)\xe2\x80\x98"
但如果代码:
\npublic static (string, string, string) SplitStr(string req)\n{\n //...\n return (A, B, C)\n}\n
Run Code Online (Sandbox Code Playgroud)\n错误消失。从错误来看,元组的括号形式和元组的括号形式Tuple<>
不同。
Tuple<>
?如何返回元组?谢谢。
\n我指的是此处描述的元组文字: https: //blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/#comment-321926
\n\n喜欢元组字面意思。
\n\n然而,我预见到很多人都会在返回元组中查找项目的顺序,并且想知道我们如何解决这个问题。
\n\n例如,将元组中的项目名称作为身份定义方面而不是顺序更有意义吗?或者有没有办法做到这一点我没有看到?
\n\n例如:假设 NextEmployee() 是一些库方法,我没有\xe2\x80\x99t 的源代码,并且\xe2\x80\x99t 没有特别详细的文档记录,假设它返回(firstname: \xe2\x80\x9cjamie\xe2\x80\x9d, lastname: \xe2\x80\x9chince\xe2\x80\x9d, id: 102348)
给我,我说:
(string lastname, var _, int __) = NextEmployee(); // I only need the last name\n
Run Code Online (Sandbox Code Playgroud)\n\n编译器要么很乐意将名字分配给姓氏,要么对此发出警告或错误。为什么不直接将姓氏映射到姓氏呢?
\n\n我\xe2\x80\x99d 看到允许更松散耦合的架构,如果我们\xe2\x80\x99t 不必记住元组中姓氏的索引,并且可以只要求一个方面\xe2\x80\x9clastname\ xe2\x80\x9d 之类的。
\n以下只是一个关于语言本身的问题,我在发布一个更好的方法来构建我的代码之前开始考虑这个问题,但它让我感兴趣.
如果我有这种结构的方法
private void Foo<T>(T bar){
//do stuff with tuples
}
Run Code Online (Sandbox Code Playgroud)
在不同的类和方法中,我有一个变量
(int first, int second) myTuple = (1,2);
Run Code Online (Sandbox Code Playgroud)
在这个变量的范围内,我可以做类似的事情
var firstValue = myTuple.first;
Run Code Online (Sandbox Code Playgroud)
无论如何,我可以传递myTuple
给Foo
维持元组内元素的命名,以便我可以做类似的事情bar.firstValue
?
我正在尝试使用反射在运行时设置ValueTuple的字段,并且遇到了问题。我怀疑我对FieldInfo.SetValue
工作原理不了解。
var a = (5, 5);
a.Item1 = 6;
Console.WriteLine(a.Item1); // 6
var t = a.GetType();
var f = t.GetField("Item1");
f.SetValue(a, 10);
Console.WriteLine(a.Item1); // 6 - Why not 10?
Run Code Online (Sandbox Code Playgroud)
有见识吗?
我最近遇到了这个:
(int, string) tst = (0, "Test");
Run Code Online (Sandbox Code Playgroud)
它背后的想法看起来很神奇,以前每当我想映射两个变量时我都会使用 KeyValuePair。看来这在 .NET Core 中可用,而不是在常规 .NET 4.7.2 上可用,这是我主要编程的地方。
我是否遗漏了某些内容,或者这仅在 .NET Core 上可用?这个变量分组的名称是什么(以便我可以进一步研究)?这和创建一个有两个变量的对象一样吗?
这是一个例子:
(int, string) tst = (0, "Test");
var (k, b) = tst;
Console.WriteLine("k: " + k);
Console.WriteLine("b: " + b);
Run Code Online (Sandbox Code Playgroud)
这是一个愚蠢的问题,但感谢您的帮助。
有一个方法接收aValueTuple
并修改后返回它。那么,我可以ValueTuple
在方法参数中指定字段名称吗?
private static void Operation()
{
var tuple = (X: 10, Y: 20);
var changeTuple = ChangeTuple(tuple);
}
private static ValueTuple<int, int> ChangeTuple(ValueTuple<int, int> tuple)
{
tuple.Item1 = 100; // ex) tuple.X = 100;
tuple.Item2 = 200; // ex) tuple.Y = 200;
return tuple;
}
Run Code Online (Sandbox Code Playgroud) valuetuple ×10
c# ×8
tuples ×6
.net ×3
c#-7.0 ×3
.net-5 ×1
arrays ×1
c#-9.0 ×1
ienumerable ×1
linq ×1