实现 IEnumerable。错误。

发布于 2024-11-26 21:25:10 字数 2640 浏览 3 评论 0原文

我的代码是:

using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;

namespace IEnumerable
{
    public class MyEnumerable<T> : IEnumerable<T>
    {
        public MyEnumerable(T[] items)
        {
            this.items = items;
        }

        public IEnumerator<T> GetEnumerator()
        {
            return new NestedEnumerator(this);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        // The enumerator definition.
        class NestedEnumerator : IEnumerator<T>
        {
            public NestedEnumerator(MyEnumerable<T> coll)
            {
                Monitor.Enter(coll.items.SyncRoot);
                this.index = -1;
                this.coll = coll;
            }

            public T Current
            {
                get { return current; }
            }

            object IEnumerator.Current
            {
                get { return Current; }
            }

            public bool MoveNext()
            {
                if (++index >= coll.items.Length)
                {
                    return false;
                }
                else
                {
                    current = coll.items[index];
                    return true;
                }
            }

            public void Reset()
            {
                current = default(T);
                index = 0;
            }

            public void Dispose()
            {
                try
                {
                    current = default(T);
                    index = coll.items.Length;
                }
                finally
                {
                    Monitor.Exit(coll.items.SyncRoot);
                }
            }

            private MyEnumerable<T> coll;
            private T current;
            private int index;
        }

        private T[] items;
    }

    public class EntryPoint
    {
        static void Main()
        {
            MyEnumerable<int> integers = new MyEnumerable<int>(new int[] { 1, 2, 3, 4 });

            foreach (int n in integers)
            {
                Console.WriteLine(n);
            }
        }
    }
}

我正在实现这段代码,但出现错误。有人能帮我做什么来消除这段代码的错误吗?请帮忙。 我的错误是:

1->'IEnumerable'是一个'命名空间',但像'类型'一样使用

2->'IEnumerable.MyEnumerable'没有实现接口成员'System.Collections.IEnumerable.GetEnumerator()'。 “IEnumerable.MyEnumerable.GetEnumerator()”无法实现“System.Collections.IEnumerable.GetEnumerator()”,因为它没有“System.Collections.IEnumerator”的匹配返回类型。

my Code is :

using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;

namespace IEnumerable
{
    public class MyEnumerable<T> : IEnumerable<T>
    {
        public MyEnumerable(T[] items)
        {
            this.items = items;
        }

        public IEnumerator<T> GetEnumerator()
        {
            return new NestedEnumerator(this);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        // The enumerator definition.
        class NestedEnumerator : IEnumerator<T>
        {
            public NestedEnumerator(MyEnumerable<T> coll)
            {
                Monitor.Enter(coll.items.SyncRoot);
                this.index = -1;
                this.coll = coll;
            }

            public T Current
            {
                get { return current; }
            }

            object IEnumerator.Current
            {
                get { return Current; }
            }

            public bool MoveNext()
            {
                if (++index >= coll.items.Length)
                {
                    return false;
                }
                else
                {
                    current = coll.items[index];
                    return true;
                }
            }

            public void Reset()
            {
                current = default(T);
                index = 0;
            }

            public void Dispose()
            {
                try
                {
                    current = default(T);
                    index = coll.items.Length;
                }
                finally
                {
                    Monitor.Exit(coll.items.SyncRoot);
                }
            }

            private MyEnumerable<T> coll;
            private T current;
            private int index;
        }

        private T[] items;
    }

    public class EntryPoint
    {
        static void Main()
        {
            MyEnumerable<int> integers = new MyEnumerable<int>(new int[] { 1, 2, 3, 4 });

            foreach (int n in integers)
            {
                Console.WriteLine(n);
            }
        }
    }
}

I am implementing this piece of code But i get an error. Can anybody help me what to do to error free this code? please help.
My Errors are :

1->'IEnumerable' is a 'namespace' but is used like a 'type'

2->'IEnumerable.MyEnumerable' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'IEnumerable.MyEnumerable.GetEnumerator()' cannot implement 'System.Collections.IEnumerable.GetEnumerator()' because it does not have the matching return type of 'System.Collections.IEnumerator'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她比我温柔 2024-12-03 21:25:11

该错误可能是因为您正在使用 IEnumerable 作为命名空间,而它已经是系统中的一种类型。

这意味着您对 IEnumerable 的引用引用的是当前命名空间,而不是您要使用的 IEnumerable (System.Collections.IEnumerable )。

尝试将您的命名空间更改为其他名称。

The error is likely that you're using IEnumerable as your namespace when it's already a type in the system.

This means that your references to IEnumerable are refering to the current namespace and not the IEnumerable you're meaning to use (System.Collections.IEnumerable).

Try changing your namespace to something else.

紫瑟鸿黎 2024-12-03 21:25:11

聆听编译器告诉您的内容并将命名空间更改为合理的名称(例如 BlingBlingWoo),

...
using System.Collections.Generic;

namespace BlingBlingWoo
{
    public class MyEnumerable<T> : IEnumerable<T>
    ...

这里发生的情况是:

错误:“IEnumerable”是“命名空间”,但使用方式类似于“类型”

您正在尝试使用类型 IEnumerable,但您已经创建了一个名为 IEnumerable 的命名空间所以编译器把它当作一个命名空间。

Listen to what the compiler is telling you and change your namespace to something sensible (like BlingBlingWoo)

...
using System.Collections.Generic;

namespace BlingBlingWoo
{
    public class MyEnumerable<T> : IEnumerable<T>
    ...

Whats happening here is:

Error : 'IEnumerable' is a 'namespace' but is used like a 'type'

You are trying to use the type IEnumerable, but you've created a namespace called IEnumerable and so the compiler things its a namespace.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文