Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[solved] c# question about Enumerator.Current

Discussion in 'Scripting' started by craig4android, Dec 21, 2019.

  1. craig4android

    craig4android

    Joined:
    May 8, 2019
    Posts:
    124
    I'm trying to implement my own IEnumerator but there is a line I don't understand:
    what is the reason for: "object IEnumerator.Current"?
    Code (CSharp):
    1. public abstract class buttonEnumerator : IEnumerator<SaveFile>
    2. {
    3.     int position;
    4.  
    5.     public SaveFile Current => throw new System.NotImplementedException();
    6.  
    7.     object IEnumerator.Current => Current; //don't understand this line
    8.  
    9.     public void Dispose()
    10.     {
    11.         throw new System.NotImplementedException();
    12.     }
    13.  
    14.     public bool MoveNext() //move to next element
    15.     {
    16.         throw new System.NotImplementedException();
    17.     }
    18.  
    19.     public void Reset()
    20.     {
    21.         position = 0;
    22.     }
    23. }
     
    Last edited: Dec 21, 2019
  2. doarp

    doarp

    Joined:
    Sep 24, 2019
    Posts:
    147
    I believe that is because IEnumerator<T> inherits from IEnumetator, which in its interface has object Current, and the generic Current<T> doesn’t satisfy the interface so you have to implement both.
     
  3. craig4android

    craig4android

    Joined:
    May 8, 2019
    Posts:
    124
    yeah, makes sense, thank you! Now I got this off my mind :)