我在for循环中定义了一个元组,像这样(来自Leetcode 1704)
for (var tuple = (i : 0, j : s.Length / 2); tuple.i < s.Length / 2 && tuple.j < s.Length; tuple.i++, tuple.j++){...}
It worked successfully .
然后我尝试显式定义元组的变量类型。
这是我在网上找到的方法
for (Tuple<int, int> tuple = new Tuple<int, int>(i : 0, j : s.Length / 2); tuple.i < s.Length / 2 && tuple.j < s.Length; tuple.i++, tuple.j++)
我得到了一些错误
Line 7: Char 58: error CS1739: The best overload for 'Tuple' does not have a parameter named 'i' (in Solution.cs)
Line 7: Char 90: error CS1061: 'Tuple<int, int>' does not contain a definition for 'i' and no accessible extension method 'i' accepting a first argument of type 'Tuple<int, int>' could be found (are you missing a using directive or an assembly reference?) (in Solution.cs)
Line 7: Char 116: error CS1061: 'Tuple<int, int>' does not contain a definition for 'j' and no accessible extension method 'j' accepting a first argument of type 'Tuple<int, int>' could be found (are you missing a using directive or an assembly reference?) (in Solution.cs)
Line 7: Char 136: error CS1061: 'Tuple<int, int>' does not contain a definition for 'i' and no accessible extension method 'i' accepting a first argument of type 'Tuple<int, int>' could be found (are you missing a using directive or an assembly reference?) (in Solution.cs)
Line 7: Char 147: error CS1061: 'Tuple<int, int>' does not contain a definition for 'j' and no accessible extension method 'j' accepting a first argument of type 'Tuple<int, int>' could be found (are you missing a using directive or an assembly reference?) (in Solution.cs)
Line 9: Char 41: error CS1061: 'Tuple<int, int>' does not contain a definition for 'i' and no accessible extension method 'i' accepting a first argument of type 'Tuple<int, int>' could be found (are you missing a using directive or an assembly reference?) (in Solution.cs)
Line 10: Char 41: error CS1061: 'Tuple<int, int>' does not contain a definition for 'j' and no accessible extension method 'j' accepting a first argument of type 'Tuple<int, int>' could be found (are you missing a using directive or an assembly reference?) (in Solution.cs)
我想知道在for循环中定义Tuple是特别的吗?我该怎么做呢
(当然,用var…我只是想弄清楚如何做到这一点而不遗漏。
Tuple通常包含属性Item1和Item2。这就是在第二个示例中出现编译错误的原因。它应该看起来像这样:
for (Tuple<int, int> tuple = new Tuple<int, int>(0, s.Length / 2);
tuple.Item1 < s.Length / 2 && tuple.Item2 < s.Length; tuple.Item1++, tuple.Item2++) { }
但是这不起作用,因为元组是不可变的,你不能增加元素。c# 6中的元组不是强类型的,这就是为什么微软在c# 7中引入了ValueTuple,它是一个可变结构体。像这样就可以了:
for (ValueTuple<int, int> tuple = new ValueTuple<int, int>(0, s.Length / 2);
tuple.Item1 < s.Length / 2 && tuple.Item2 < s.Length; tuple.Item1++, tuple.Item2++) { }
如果你只是想在你的示例中避免使用var关键字,你可以这样声明它:
for ((int i, int j) tuple = (i: 0, j: s.Length / 2);
tuple.i < s.Length / 2 && tuple.j < s.Length; tuple.i++, tuple.j++) { }
>你在循环中定义的实际上不是System.Tuple<T1,T2>
,它实际上是一种不同的东西,称为ValueTuple
,查看这篇现有的文章,了解差异:
System和System之间的区别是什么?ValueTuple和System.Tuple?
在哪里定义元组并不重要。重要的是你如何定义。下面是正确的定义(项目类型为string
和int
):
Tuple<int, string> tuple = Tuple.Create<int, string>(0, "a string"); // Generic parameters are types of the elements
// Accessing the elements
int element1 = tuple.Item1;
一个元组最多可以存储8个不同类型的元素。