尝试执行我自己的 zip 操作,得到了此代码(通用类型参数 T 在此方法的封闭类上定义)。
public List<(T, T2)> Zip<T, T2>(SQLList<T2> that)
where T2 : LDBSQLemitter {
var res = this.Zip(that).ToList();
return res;
}
错误是
Error CS0029 Cannot implicitly convert type 'System.Collections.Generic.List<(T First, T2 Second)>' to 'System.Collections.Generic.List<(T, T2)>'
“System.Collections.Generic.List<(T First, T2 Second)>”中的第一/第二个内容是什么,为什么“System.Collections.Generic.List<(T, T2)>”中缺少它- - 我在这里不明白元组类型是什么?
好的,使用try代替对每个元组进行显式转换,所以
public List<(T, T2)> Zip<T, T2>(SQLList<T2> that)
where T2 : LDBSQLemitter {
var tmp = this.Zip(that).ToList();
var res = new List<(T, T2)>();
foreach (var x in tmp) {
res.Add((x.First, x.Second));
}
return res;
}
现在这个错误完全令人困惑,显然是在提示它无法在两个相同的元组类型之间进行转换:
Error CS1503 Argument 1: cannot convert from '(T, T2)' to '(T, T2)'
瓦萨普?
最佳答案
问题是this
,编译器无法关联 T
.
了解类的签名也会有所帮助。是否还包含通用参数 T
?同时拥有 T
您定义的方法中的通用参数 T
两次,而且不一样。
您可以通过显式强制转换来修复它
var res = ((IEnumerable<T>)this).Zip(that).ToList();
或添加 Actor
var res = this.Cast<T>().Zip(that).ToList();
或者去掉方法中多余的T
public List<(T, T2)> Zip<T2>(SQLList<T2> that)
关于C# 通用错误,提示它无法在相同的元组类型之间进行转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68509910/