C# Class 泛型实现

C# Class implementation with generics

大家好,我正在学习 C#,但 运行 遇到了一些编译器错误: 我收到错误消息:'LinkedList' 没有实现接口成员 'IEnumerable.GetEnumerator()'

我想我做到了。

代码如下:

using System;
using System.Collections.Generic;

namespace LinkedListGenericsExample
{
     public class LinkedListNode<T>
     {
         //constructor
         public LinkedListNode(T value)
         {
             //code here
         }
         //code here
     }

     //LinkedList class with generics. It inherit the IEnumerable class with 
     //generics. Should I use IEnumerable or IEnumerable<T>?
     public class LinkedList<T>: IEnumerable<T>
     {
         //code here
     }

     public LinkedListNode<T> AddLast(T node)
     {
         //code here
     }

     public IEnumerator<T> GetEnumerator()
     {
         //code here
     }

     //here I think the GetEnumerator() method is implemented
     IEnumerator IEnumerable.GetEnumerator()
     {
         return GetEnumerator();
     }

     //Trying this but not working. Also I am confused.
     /*
     IEnumerator IEnumerable<T>.GetEnumerator()
     {
         return GetEnumerator();
     }
     */

     //Main() below
}

我正在使用Visual Studio代码编译代码。 遇到错误:

'LinkedList'没有实现接口成员'IEnumerable.GetEnumerator()'

使用通用类型'IEnumerator'需要 1 个类型参数

使用通用类型'IEnumerable'需要 1 个类型参数

'IEnumerable' in explicit interface declaration is not an interface

问题:

1) 我应该用泛型继承 IEnumerable class 还是 IEnumerable class?

2) 我如何实现 "IEnumerable.GetEnumerator()" 看起来编译器无法识别我的 GetEnumerator() 实现,但我不确定为什么....

这里需要一些帮助。谢谢!

正在更新下面的完整代码。有效!!

using System;
using System.Collections; //using System.Collections instead

namespace LinkedListGenericsExample
{
    //Linked list node class in Generics form
    public class LinkedListNode<T> 
    {
        //LinkedListNode constructor
        public LinkedListNode(T value)
        {
            this.Value = value;
        }

        public T Value; 
        public LinkedListNode<T> Next {get; internal set;} 
        public LinkedListNode<T> Prev {get; internal set;}

    }

    public class LinkedList<T>: IEnumerable  
    {
        public LinkedListNode<T> First {get; private set;}
        public LinkedListNode<T> Last {get; private set;}

        public LinkedListNode<T> AddLast(T node)
        {
            var newNode = new LinkedListNode<T>(node);
            if (First == null)
            {
                First = newNode;
                Last = First;
            }
            else
            {
                Last.Next = newNode;
                Last = newNode;
            }

            return newNode; 
        } 

        public IEnumerator GetEnumerator()  
        {
            LinkedListNode<T> current = First;

            while(current != null)
            {
                yield return current.Value;
                current = current.Next;
            }      
        }

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

        /*
        IEnumerator IEnumerable<T>.GetEnumerator()
        {
        }
        */
    }
    class Program
    {

        static void Main(string[] args)
        {
            //Console.WriteLine("Hello World!");
            var list2 = new LinkedList<int>();
            var list3 = new LinkedList<String>();
            list2.AddLast(1);
            list2.AddLast(3);
            list2.AddLast(5);

            //Go throuhg entire list of numbers
            foreach(int i in list2)
            {
                Console.WriteLine(i);
            }

            Console.WriteLine();

            list3.AddLast("2");
            list3.AddLast("four");
            list3.AddLast("foo"); 

            //Go through entire list of strings
            foreach(string s in list3)
            {
                Console.WriteLine(s);
            }

        }
    }
}

关于你的两个问题,这里是2美分。 1.我建议你实现通用版本。这将确保类型安全和其他好处。您可以在 link. 中阅读更多关于泛型优点的信息。由于您正在学习 C#,因此阅读它是个好主意。

  1. 您的实现看起来 good.Please 在您的代码中添加对 System.Collections 命名空间的引用以修复编译错误。

    使用 System.Collections;