IEnumerable / IEnumerator中的C#7错误。 [英] C#7 bug in IEnumerable/IEnumerator.
问题描述
以下代码是从 C#6.0 in Nutshell 的第292页逐字记录的。 代码编译并在C#5/6(使用LINQPad 5)中正常工作。然而,在使用C#7的Visual Studio中,所有地狱都破裂了。编译器会产生大量关于
的错误,这些错误是GetEnumerator()和Current()的显式/隐式接口定义。在C#7中,可以修改代码以使其工作吗?
The following code is taken verbatim from page 292 of C# 6.0 in a Nutshell. The code compiles and works just fine in C#5/6 (with LINQPad 5). However all hell breaks loose in visual studio with C# 7. The compiler generates numerous errors concerning the explicit/implicit interface definitions of GetEnumerator() and Current(). In C# 7, can the code be modified to work?
class MyIntList:IEnumerable< int>
{
&NBSP;&NBSP;&NBSP; int [] data = {1,2,3}; // C#6中的私人OK。在C#7中不行。提供什么?
&NBSP;&NBSP;&NBSP; public IEnumerator< int> GetEnumerator(){return new Enumerator(this); }¥b $ b &NBSP;&NBSP;&NBSP; IEnumerator IEnumerable.GetEnumerator(){return new Enumerator(this); } //在C#7.0中,所有地狱都破裂了。
&NBSP;&NBSP;&NBSP; class Enumerator:IEnumerator< int>
&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; int index = -1;
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; MyIntList集合;
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; public Enumerator(MyIntList collection)
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; this.collection = collection;
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; }
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; public int Current => collection.data [指数];&NBSP;&NBSP; //在C#7中,所有地狱都破裂了。
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; object IEnumerator.Current => collection.data [指数]; //在C#7中,所有地狱都破裂了。
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;
class MyIntList : IEnumerable<int>
{
int[] data = { 1, 2, 3 }; // Private OK in C#6. Not OK in C# 7. What gives?
public IEnumerator<int> GetEnumerator() { return new Enumerator(this); }
IEnumerator IEnumerable.GetEnumerator() { return new Enumerator(this); } // All hell breaks loose in C# 7.0.
class Enumerator : IEnumerator<int>
{
int index = -1;
MyIntList collection;
public Enumerator(MyIntList collection)
{
this.collection = collection;
}
public int Current => collection.data[index]; // All hell breaks loose in C# 7.
object IEnumerator.Current => collection.data[index]; // All hell breaks loose in C# 7.
public bool MoveNext()=> ++ index< collection.data.Length;
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; public void Reset()=> index = -1;
&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; void IDisposable.Dispose(){}
&NBSP;&NBSP;&NBSP; &NBSP;}&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;
}
public bool MoveNext() => ++index < collection.data.Length;
public void Reset() => index = -1;
void IDisposable.Dispose() { }
}
}
meouzer
推荐答案
MeOuzer,
MeOuzer,
如果有一个'突破性变化',这个论坛无法帮助你(除了解决方法)。
If there is a 'breaking change' than this forum can not help you (beside with work arounds).
你可以通过在Visual Studio中做到 - >>帮助 - > FeedBack - >报告问题
You can tell it by doing in Visual Studio -> Help -> FeedBack -> Report a problem
这篇关于IEnumerable / IEnumerator中的C#7错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!