以下代码是从 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> { int [] data = {1,2,3}; // C#6中的私人OK。在C#7中不行。提供什么? public IEnumerator< int> GetEnumerator(){return new Enumerator(this); }¥b $ b IEnumerator IEnumerable.GetEnumerator(){return new Enumerator(this); } //在C#7.0中,所有地狱都破裂了。 class Enumerator:IEnumerator< int> { int index = -1; MyIntList集合; public Enumerator(MyIntList collection) { this.collection = collection; } public int Current => collection.data [指数]; //在C#7中,所有地狱都破裂了。 object IEnumerator.Current => collection.data [指数]; //在C#7中,所有地狱都破裂了。
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; public void Reset()=> index = -1; void IDisposable.Dispose(){} } }
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错误。
发布评论