为什么 ValueTuple.Create 8 元组重载将最后一个参数包装在另一个 ValueTuple 中?
Why does the ValueTuple.Create 8-tuple overload wrap the last parameter in another ValueTuple?
查看 System.ValueTuple
的源代码(使用 JetBrains Rider 2019.1.2 反编译)我注意到创建 8 元组的方法将最后一个参数包装在它自己的元组中。将元素包装在 1 元组中有什么意义?
public static ValueTuple<T1, T2, T3, T4, T5, T6, T7, ValueTuple<T8>> Create<T1, T2, T3, T4, T5, T6, T7, T8>(
T1 item1,
T2 item2,
T3 item3,
T4 item4,
T5 item5,
T6 item6,
T7 item7,
T8 item8)
{
return new ValueTuple<T1, T2, T3, T4, T5, T6, T7, ValueTuple<T8>>(item1, item2, item3, item4, item5, item6, item7, ValueTuple.Create<T8>(item8));
}
反编译头文件:
// Decompiled with JetBrains decompiler
// Type: System.ValueTuple
// Assembly: System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
// MVID: CE11CF72-ED8D-4122-8743-9D6985631221
// Assembly location: C:\Program Files\dotnet\shared\Microsoft.NETCore.App.0.0-preview3-27503-5\System.Private.CoreLib.dll
A ValueTuple
是一种能够存储 7 个值并表示 'and I will put the leftovers in another ValueType here'(即 Rest
属性)的类型。
I still don't understand what is the advantage of wrapping the last
parameter in another ValueTuple. For example, why not force the
programmer declare a custom type and use it instead?
有两个原因:
a) 如果你这样做,你实际上从 7 种类型/值变成了 8 种类型/值。但这并不能解决问题。 9呢? 10?
b) Rest
为 ValueType
意味着您可以支持任意数量的类型。处理前 7 个的代码可以与处理后 7 个等的代码相同。因为它一直是 ValueType
。
此外,请注意,如果您已经将 ValueType
作为八个参数,请调用构造函数而不是 Tuple.Create
(以避免将 ValueTuple
包装在另一个 ValueTuple
).
查看 System.ValueTuple
的源代码(使用 JetBrains Rider 2019.1.2 反编译)我注意到创建 8 元组的方法将最后一个参数包装在它自己的元组中。将元素包装在 1 元组中有什么意义?
public static ValueTuple<T1, T2, T3, T4, T5, T6, T7, ValueTuple<T8>> Create<T1, T2, T3, T4, T5, T6, T7, T8>(
T1 item1,
T2 item2,
T3 item3,
T4 item4,
T5 item5,
T6 item6,
T7 item7,
T8 item8)
{
return new ValueTuple<T1, T2, T3, T4, T5, T6, T7, ValueTuple<T8>>(item1, item2, item3, item4, item5, item6, item7, ValueTuple.Create<T8>(item8));
}
反编译头文件:
// Decompiled with JetBrains decompiler
// Type: System.ValueTuple
// Assembly: System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
// MVID: CE11CF72-ED8D-4122-8743-9D6985631221
// Assembly location: C:\Program Files\dotnet\shared\Microsoft.NETCore.App.0.0-preview3-27503-5\System.Private.CoreLib.dll
A ValueTuple
是一种能够存储 7 个值并表示 'and I will put the leftovers in another ValueType here'(即 Rest
属性)的类型。
I still don't understand what is the advantage of wrapping the last parameter in another ValueTuple. For example, why not force the programmer declare a custom type and use it instead?
有两个原因:
a) 如果你这样做,你实际上从 7 种类型/值变成了 8 种类型/值。但这并不能解决问题。 9呢? 10?
b) Rest
为 ValueType
意味着您可以支持任意数量的类型。处理前 7 个的代码可以与处理后 7 个等的代码相同。因为它一直是 ValueType
。
此外,请注意,如果您已经将 ValueType
作为八个参数,请调用构造函数而不是 Tuple.Create
(以避免将 ValueTuple
包装在另一个 ValueTuple
).