Le réseau pour les développeurs > Forums - Accueil > Visual C# General > Constraint.LinkedList<K,T>' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'Constraint.LinkedList<K,T>.GetEnumerator()' cannot implement 'System.Collections.IEnumerable.GetEnumerator()' because it does not have the m
Poser une questionPoser une question
 

TraitéeConstraint.LinkedList<K,T>' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'Constraint.LinkedList<K,T>.GetEnumerator()' cannot implement 'System.Collections.IEnumerable.GetEnumerator()' because it does not have the m

  • dimanche 8 novembre 2009 10:05cutedevil Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    How to correct it? The code is from a sample on MSDN

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;

    namespace Constraint
    {
        class Node<K, T>
        {
            public K Key;
            public T Item;
            public Node<K, T> NextNode;
            public Node()
            {
                Key = default(K);
                Item = default(T);
                NextNode = null;
            }
            public Node(K key, T item, Node<K, T> nextNode)
            {
                Key = key;
                Item = item;
                NextNode = nextNode;
            }
        }

     

        public class LinkedList<K, T> : IEnumerable<T> where K : IComparable<K>
        {
            Node<K, T> m_Head;

            public LinkedList()
            {
                m_Head = new Node<K, T>();
            }

            public void AddHead(K key, T item)
            {
                Node<K, T> newNode = new Node<K, T>(key, item, m_Head.NextNode);
                m_Head.NextNode = newNode;
            }

            public T this[K key]
            {
                get
                {
                    return Find(key);
                }
            }

            T Find(K key)
            {
                Node<K, T> current = m_Head;
                while (current.NextNode != null)
                {
                    if (current.Key.CompareTo(key) == 0)
                    {
                        break;
                    }
                    else
                    {
                        current = current.NextNode;
                    }
                }
                return current.Item;
            }

            public IEnumerator<T> GetEnumerator()
            {
                Node<K, T> current = m_Head;
                while (current != null)
                {
                    yield return current.Item;
                    current = current.NextNode;
                }
            }

            public static LinkedList<K, T> operator +(LinkedList<K, T> lhs, LinkedList<K, T> rhs)
            {
                return concatenate(lhs, rhs);
            }

            static LinkedList<K, T> concatenate(LinkedList<K, T> list1, LinkedList<K, T> list2)
            {
                LinkedList<K, T> newList = new LinkedList<K, T>();
                Node<K, T> current;

                current = list1.m_Head;
                while (current != null)
                {
                    newList.AddHead(current.Key, current.Item);
                    current = current.NextNode;
                }
                current = list2.m_Head;
                while (current != null)
                {
                    newList.AddHead(current.Key, current.Item);
                    current = current.NextNode;
                }
                return newList;
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                LinkedList<int,string>list = new LinkedList<int,string>();
                list.AddHead(123, "AAA");
                list.AddHead(244, "BBB");
                string item = list[123];           
            }
        }
    }
          


     

     

Réponses

  • dimanche 8 novembre 2009 10:57Morantex Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     TraitéeA du code

    How to correct it? The code is from a sample on MSDN

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;

    namespace Constraint
    {
        class Node<K, T>
        {
            public K Key;
            public T Item;
            public Node<K, T> NextNode;
            public Node()
            {
                Key = default(K);
                Item = default(T);
                NextNode = null;
            }
            public Node(K key, T item, Node<K, T> nextNode)
            {
                Key = key;
                Item = item;
                NextNode = nextNode;
            }
        }

     

        public class LinkedList<K, T> : IEnumerable<T> where K : IComparable<K>
        {
            Node<K, T> m_Head;

            public LinkedList()
            {
                m_Head = new Node<K, T>();
            }

            public void AddHead(K key, T item)
            {
                Node<K, T> newNode = new Node<K, T>(key, item, m_Head.NextNode);
                m_Head.NextNode = newNode;
            }

            public T this[K key]
            {
                get
                {
                    return Find(key);
                }
            }

            T Find(K key)
            {
                Node<K, T> current = m_Head;
                while (current.NextNode != null)
                {
                    if (current.Key.CompareTo(key) == 0)
                    {
                        break;
                    }
                    else
                    {
                        current = current.NextNode;
                    }
                }
                return current.Item;
            }

            public IEnumerator<T> GetEnumerator()
            {
                Node<K, T> current = m_Head;
                while (current != null)
                {
                    yield return current.Item;
                    current = current.NextNode;
                }
            }

            public static LinkedList<K, T> operator +(LinkedList<K, T> lhs, LinkedList<K, T> rhs)
            {
                return concatenate(lhs, rhs);
            }

            static LinkedList<K, T> concatenate(LinkedList<K, T> list1, LinkedList<K, T> list2)
            {
                LinkedList<K, T> newList = new LinkedList<K, T>();
                Node<K, T> current;

                current = list1.m_Head;
                while (current != null)
                {
                    newList.AddHead(current.Key, current.Item);
                    current = current.NextNode;
                }
                current = list2.m_Head;
                while (current != null)
                {
                    newList.AddHead(current.Key, current.Item);
                    current = current.NextNode;
                }
                return newList;
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                LinkedList<int,string>list = new LinkedList<int,string>();
                list.AddHead(123, "AAA");
                list.AddHead(244, "BBB");
                string item = list[123];           
            }
        }
    }
          


     

     


    You forgot to implement the non-generic version of GetEnumerator.

    Here is the corrected code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    
    namespace Constraint
    {
        class Node<K, T>
        {
            public K Key;
            public T Item;
            public Node<K, T> NextNode;
            public Node()
            {
                Key = default(K);
                Item = default(T);
                NextNode = null;
            }
            public Node(K key, T item, Node<K, T> nextNode)
            {
                Key = key;
                Item = item;
                NextNode = nextNode;
            }
        }
    
    
    
        public class LinkedList<K, T> : IEnumerable<T> where K : IComparable<K>
        {
            Node<K, T> m_Head;
    
            public LinkedList()
            {
                m_Head = new Node<K, T>();
            }
    
            public void AddHead(K key, T item)
            {
                Node<K, T> newNode = new Node<K, T>(key, item, m_Head.NextNode);
                m_Head.NextNode = newNode;
            }
    
            public T this[K key]
            {
                get
                {
                    return Find(key);
                }
            }
    
            T Find(K key)
            {
                Node<K, T> current = m_Head;
                while (current.NextNode != null)
                {
                    if (current.Key.CompareTo(key) == 0)
                    {
                        break;
                    }
                    else
                    {
                        current = current.NextNode;
                    }
                }
                return current.Item;
            }
    
            public IEnumerator<T> GetEnumerator()
            {
                Node<K, T> current = m_Head;
                while (current != null)
                {
                    yield return current.Item;
                    current = current.NextNode;
                }
            }
    
            public static LinkedList<K, T> operator +(LinkedList<K, T> lhs, LinkedList<K, T> rhs)
            {
                return concatenate(lhs, rhs);
            }
    
            static LinkedList<K, T> concatenate(LinkedList<K, T> list1, LinkedList<K, T> list2)
            {
                LinkedList<K, T> newList = new LinkedList<K, T>();
                Node<K, T> current;
    
                current = list1.m_Head;
                while (current != null)
                {
                    newList.AddHead(current.Key, current.Item);
                    current = current.NextNode;
                }
                current = list2.m_Head;
                while (current != null)
                {
                    newList.AddHead(current.Key, current.Item);
                    current = current.NextNode;
                }
                return newList;
            }
    
            #region IEnumerable Members
    
            IEnumerator IEnumerable.GetEnumerator()
            {
                throw new NotImplementedException();
            }
    
            #endregion
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                LinkedList<int, string> list = new LinkedList<int, string>();
                list.AddHead(123, "AAA");
                list.AddHead(244, "BBB");
                string item = list[123];
            }
        }
    }
    
    
    

    If you open your project, right-click the class name "LinkedList" then select "Implement Interface" the IDE will add this empty interface member for you.

    Hugh




    Hugh Moran - http://www.morantex.com
  • vendredi 13 novembre 2009 07:30Harry ZhuMSFT, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    Hi,

    The nongeneric IEnumerator is required when implementing generic IEnumerable.

    The following is experpted from: http://msdn.microsoft.com/en-us/library/78dfe2yb.aspx

    Notes to Implementers:

    Implementing this interface requires implementing the nongeneric IEnumerator interface. The MoveNext and Reset methods do not depend on T, and appear only on the nongeneric interface. The Current property appears on both interfaces, and has different return types. Implement the nongeneric IEnumerator..::.Current property as an explicit interface implementation. This allows any consumer of the nongeneric interface to consume the generic interface.

    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

Toutes les réponses

  • dimanche 8 novembre 2009 10:57Morantex Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     TraitéeA du code

    How to correct it? The code is from a sample on MSDN

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;

    namespace Constraint
    {
        class Node<K, T>
        {
            public K Key;
            public T Item;
            public Node<K, T> NextNode;
            public Node()
            {
                Key = default(K);
                Item = default(T);
                NextNode = null;
            }
            public Node(K key, T item, Node<K, T> nextNode)
            {
                Key = key;
                Item = item;
                NextNode = nextNode;
            }
        }

     

        public class LinkedList<K, T> : IEnumerable<T> where K : IComparable<K>
        {
            Node<K, T> m_Head;

            public LinkedList()
            {
                m_Head = new Node<K, T>();
            }

            public void AddHead(K key, T item)
            {
                Node<K, T> newNode = new Node<K, T>(key, item, m_Head.NextNode);
                m_Head.NextNode = newNode;
            }

            public T this[K key]
            {
                get
                {
                    return Find(key);
                }
            }

            T Find(K key)
            {
                Node<K, T> current = m_Head;
                while (current.NextNode != null)
                {
                    if (current.Key.CompareTo(key) == 0)
                    {
                        break;
                    }
                    else
                    {
                        current = current.NextNode;
                    }
                }
                return current.Item;
            }

            public IEnumerator<T> GetEnumerator()
            {
                Node<K, T> current = m_Head;
                while (current != null)
                {
                    yield return current.Item;
                    current = current.NextNode;
                }
            }

            public static LinkedList<K, T> operator +(LinkedList<K, T> lhs, LinkedList<K, T> rhs)
            {
                return concatenate(lhs, rhs);
            }

            static LinkedList<K, T> concatenate(LinkedList<K, T> list1, LinkedList<K, T> list2)
            {
                LinkedList<K, T> newList = new LinkedList<K, T>();
                Node<K, T> current;

                current = list1.m_Head;
                while (current != null)
                {
                    newList.AddHead(current.Key, current.Item);
                    current = current.NextNode;
                }
                current = list2.m_Head;
                while (current != null)
                {
                    newList.AddHead(current.Key, current.Item);
                    current = current.NextNode;
                }
                return newList;
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                LinkedList<int,string>list = new LinkedList<int,string>();
                list.AddHead(123, "AAA");
                list.AddHead(244, "BBB");
                string item = list[123];           
            }
        }
    }
          


     

     


    You forgot to implement the non-generic version of GetEnumerator.

    Here is the corrected code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    
    namespace Constraint
    {
        class Node<K, T>
        {
            public K Key;
            public T Item;
            public Node<K, T> NextNode;
            public Node()
            {
                Key = default(K);
                Item = default(T);
                NextNode = null;
            }
            public Node(K key, T item, Node<K, T> nextNode)
            {
                Key = key;
                Item = item;
                NextNode = nextNode;
            }
        }
    
    
    
        public class LinkedList<K, T> : IEnumerable<T> where K : IComparable<K>
        {
            Node<K, T> m_Head;
    
            public LinkedList()
            {
                m_Head = new Node<K, T>();
            }
    
            public void AddHead(K key, T item)
            {
                Node<K, T> newNode = new Node<K, T>(key, item, m_Head.NextNode);
                m_Head.NextNode = newNode;
            }
    
            public T this[K key]
            {
                get
                {
                    return Find(key);
                }
            }
    
            T Find(K key)
            {
                Node<K, T> current = m_Head;
                while (current.NextNode != null)
                {
                    if (current.Key.CompareTo(key) == 0)
                    {
                        break;
                    }
                    else
                    {
                        current = current.NextNode;
                    }
                }
                return current.Item;
            }
    
            public IEnumerator<T> GetEnumerator()
            {
                Node<K, T> current = m_Head;
                while (current != null)
                {
                    yield return current.Item;
                    current = current.NextNode;
                }
            }
    
            public static LinkedList<K, T> operator +(LinkedList<K, T> lhs, LinkedList<K, T> rhs)
            {
                return concatenate(lhs, rhs);
            }
    
            static LinkedList<K, T> concatenate(LinkedList<K, T> list1, LinkedList<K, T> list2)
            {
                LinkedList<K, T> newList = new LinkedList<K, T>();
                Node<K, T> current;
    
                current = list1.m_Head;
                while (current != null)
                {
                    newList.AddHead(current.Key, current.Item);
                    current = current.NextNode;
                }
                current = list2.m_Head;
                while (current != null)
                {
                    newList.AddHead(current.Key, current.Item);
                    current = current.NextNode;
                }
                return newList;
            }
    
            #region IEnumerable Members
    
            IEnumerator IEnumerable.GetEnumerator()
            {
                throw new NotImplementedException();
            }
    
            #endregion
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                LinkedList<int, string> list = new LinkedList<int, string>();
                list.AddHead(123, "AAA");
                list.AddHead(244, "BBB");
                string item = list[123];
            }
        }
    }
    
    
    

    If you open your project, right-click the class name "LinkedList" then select "Implement Interface" the IDE will add this empty interface member for you.

    Hugh




    Hugh Moran - http://www.morantex.com
  • dimanche 8 novembre 2009 14:25cutedevil Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Can you esplain the code "#region IEnumerable Members

            IEnumerator IEnumerable.GetEnumerator()
            {
                throw new NotImplementedException();
            }

            #endregion"

    I don't know why it is necessary.

     . Thank you
  • vendredi 13 novembre 2009 07:30Harry ZhuMSFT, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    Hi,

    The nongeneric IEnumerator is required when implementing generic IEnumerable.

    The following is experpted from: http://msdn.microsoft.com/en-us/library/78dfe2yb.aspx

    Notes to Implementers:

    Implementing this interface requires implementing the nongeneric IEnumerator interface. The MoveNext and Reset methods do not depend on T, and appear only on the nongeneric interface. The Current property appears on both interfaces, and has different return types. Implement the nongeneric IEnumerator..::.Current property as an explicit interface implementation. This allows any consumer of the nongeneric interface to consume the generic interface.

    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.