Ambiguity in interfaces - C#

Shervan360 1,681 Reputation points
2023-08-14T08:29:58.8933333+00:00

Hello,

Why can't I public a member of an interface when I explicitly write the name of the interface?

Please see:

Screenshot 2023-08-14 012434

Screenshot 2023-08-14 012211

   public class CountDown : IEnumerator
   {
        int count = 11;
        object IEnumerator.Current => count;

        public bool IEnumerator.MoveNext() // ERROR
        {
             return count-- > 0;
        }

        void IEnumerator.Reset()
        {
             throw new NotImplementedException();
        }
   }
using System.Collections;

namespace ConsoleApp1
{
     public class CountDown : IEnumerator
     {
          int count = 11;
          object IEnumerator.Current => count;

          public bool MoveNext() // OK
          {
               return count-- > 0;
          }

          void IEnumerator.Reset()
          {
               throw new NotImplementedException();
          }
     }
     internal class Program
     {
          static void Main(string[] args)
          {
               CountDown CD = new();
               Console.WriteLine();
          }
     }
}

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. Bruce (SqlWork.com) 82,061 Reputation points Volunteer Moderator
    2023-08-14T15:25:39.6933333+00:00

    Because it’s a member of the class’s interface implementation, not the class. When an interface implementation contains the interface name prefix, it’s only callable when the instance is cast to the interface.

    interfaces were designed to implement multi-inheritance. the code supplies the common implementation. the public definition is the common, but you can supply implementations just for when cast to an interface,

         public class CountDown : IEnumerator
         {
              int count = 11;
              object IEnumerator.Current => count;
    
              // public implementation 
              public bool MoveNext() // OK
              {
                   return count-- > 0;
              }
              // implementation used if cast to IEnumerator
              bool IEnumerator.MoveNext()
              {
                 return count-- > 0;
              }
              void IEnumerator.Reset()
              {
                   throw new NotImplementedException();
              }
         }
    

    note: the main use case of explicit interface implementation is when you don't want the method to be available unless cast to the interface. this would happen if you implemented two interfaces with the same signature, but need different implementations.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.