IEnumerable / IEnumerator中的C#7错误。

编程入门 行业动态 更新时间:2024-10-22 20:27:00
本文介绍了IEnumerable / IEnumerator中的C#7错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下代码是从 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错误。

本文发布于:2023-11-03 15:42:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555478.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   IEnumerable   IEnumerator

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!