Why this simple generic code does not compile?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ympostor

    #1

    Why this simple generic code does not compile?

    public class TestGenericEnum erator<T:
    System.Collecti ons.Generic.IEn umerable<T>
    {
    public System.Collecti ons.Generic.IEn umerator<TGetEn umerator()
    {
    return null;
    }
    }


    I don't understand why this is returning:

    Error 6 'TestGenericEnu merator<T>' does not implement interface member
    'System.Collect ions.Generic.IE numerable<T>.Ge tEnumerator()'.
    'TestGenericEnu merator<T>.GetE numerator()' is either static, not public,
    or has the wrong return type.

    Anyone? :(
  • Marc Gravell

    #2
    Re: Why this simple generic code does not compile?

    Because IEnumerable<T: IEnumerable, and you haven't implemented the
    non-generic form; just add:

    System.Collecti ons.IEnumerator
    System.Collecti ons.IEnumerable .GetEnumerator( ) {
    return GetEnumerator() ;
    }

    Comment

    • Peter Morris

      #3
      Re: Why this simple generic code does not compile?

      public System.Collecti ons.Generic.IEn umerator<TGetEn umerator()

      Try this

      public System.Collecti ons.Generic.IEn umerator<TGetEn umerator<T>()



      Comment

      • Marc Gravell

        #4
        Re: Why this simple generic code does not compile?

        Try this
        public System.Collecti ons.Generic.IEn umerator<TGetEn umerator<T>()
        The T in this method is now a different T to the class's template T
        (which the compiler issues a CSO693 warning about) - but this means
        that the above doesn't implement the interface, since you could use:

        foreach(int i in new
        TestGenericEnum erator<string>( ).GetEnumerator <int>()) {...}

        which doesn't do the job of implementing IEnumerable<str ing>. Good
        idea, though.

        Marc

        Comment

        • Ympostor

          #5
          Re: Why this simple generic code does not compile?

          Marc Gravell escribió:
          Because IEnumerable<T: IEnumerable, and you haven't implemented the
          non-generic form; just add:
          >
          System.Collecti ons.IEnumerator
          System.Collecti ons.IEnumerable .GetEnumerator( ) {
          return GetEnumerator() ;
          }
          >

          Thanks! Now it works, but, now I don't understand why I need this cast:

          public class TestGenericEnum erator<T:
          System.Collecti ons.Generic.IEn umerable<T>
          {
          System.Collecti ons.Generic.IEn umerator<T>
          System.Collecti ons.Generic.IEn umerable<T>.Get Enumerator()
          {
          return null;
          }

          System.Collecti ons.IEnumerator
          System.Collecti ons.IEnumerable .GetEnumerator( )
          {
          return (

          //why is this cast needed!?
          (IEnumerable<T> )
          //end cast

          this).GetEnumer ator();
          }
          }

          Thanks in advance.

          Comment

          • Marc Gravell

            #6
            Re: Why this simple generic code does not compile?

            why is this cast needed!?

            Because you switched the generic version to an explicit
            implementation, and so there is no this.GetEnumera tor()

            If you want to do this, consider having a single private
            implementation that both the generic and non-generic versions invoke:

            private IEnumerator<TGe tEnumerator() {
            return null; // the actual implementation
            }
            IEnumerator<TIE numerable<T>.Ge tEnumerator() {
            return GetEnumerator() ;
            }
            IEnumerator IEnumerable.Get Enumerator() {
            return GetEnumerator() ;
            }

            Comment

            Working...