Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Can A C# Class Inherit From One Interface Both Implicitly and Explicitly?

Today I happens to find that one C# class can inherit one interface both in implicit and explicit way. This surprises me. If C# works in this way, then one instance can behave differently when referenced in different way.

interface IFoo
{
    void DoSomething();
}

class Foo : IFoo
{
    #region IFoo Members
    public void DoSomething()
    {
        Console.WriteLine("do something implicitly");
    }
    #endregion

    #region IFoo Members
    void IFoo.DoSomething()
    {
        Console.WriteLine("do something explicitly");
    }
    #endregion
}


        Foo f = new Foo();
        f.DoSomething();

        ((IFoo)f).DoSomething();

Above code runs and output

do something implicitly
do something explicitly

I believe that this design of C# make inconsistency of behavior. Perhaps it is mandatory that one C# class can inherit from one interface in implicit or expliict way, but not both.

Is there any reason that why C# is designed in such a way?

like image 847
Morgan Cheng Avatar asked Oct 31 '08 10:10

Morgan Cheng


People also ask

Why is AC used instead of DC?

Alternating current (AC) is the type of electric current generated by the vast majority of power plants and used by most power distribution systems. Alternating current is cheaper to generate and has fewer energy losses than direct current when transmitting electricity over long distances.

Why does AC current alternate?

As the wire spins and periodically enters a different magnetic polarity, the voltage and current alternate on the wire. This current can change direction periodically, and the voltage in an AC circuit also periodically reverses because the current changes direction.

Why can't you put AC in batteries instead of DC?

Why we can't store AC in Batteries instead of DC.or Can we store AC in batteries instead of DC? We cannot store AC in batteries because AC changes their polarity upto 50 (When frequency = 50 Hz) or 60 (When frequency = 60 Hz) times in a second.

Why is AC used to transmit electricity?

AC is used for long distance power transmission instead of DC because it is more convenient to step up/down voltage for AC than DC.


4 Answers

Every class that implements an interface has a mapping between that class's members and the interface's members. If the class explicitly implements an interface member, then the explicit implementation will always be mapped to the interface. If there isn't an explicit implementation then an implicit implementation is expected, and that one will be mapped to the interface.

When a class has the same member name and associated types as an interface but it also explicitly implements the corresponding member for the interface, then the class's "implicit" implementation isn't considered an implementation of the interface at all (unless the explicit implementation calls it).

In addition to different meanings in each case where the class implements multiple interfaces with the same member name/types, even with only one interface, the class itself is considered to have an implicit interface which might have the same member/types as the sole interface but still mean something different.

like image 180
Mark Cidade Avatar answered Oct 05 '22 17:10

Mark Cidade


Your example does not implement IFoo both implicitly and explicitly. You only implement IFoo.DoSometing() explicitly. You have a new method on your class called DoSomething(). It has nothing to do with IFoo.DoSomething, except that it has the same name and parameters.

like image 30
Hallgrim Avatar answered Oct 05 '22 17:10

Hallgrim


This makes it more flexible for when there are collisions. In particular, look at IEnumerator and IEnumerator<T> - they both have a Current property, but of different types. You have to use explicit interface implementation in order to implement both (and the generic form extends the non-generic form).

like image 45
Jon Skeet Avatar answered Oct 05 '22 17:10

Jon Skeet


Multiple inheritance : What if you derive from two interfaces defining same method for different purposes?

  interface IMoveable
  {
    public void Act();
  }

  interface IRollable
  {
    public void Act();
  }

  class Thing : IMoveable, IRollable
  {
    //TODO Roll/Move code here

    void IRollable.Act()
    {
      Roll();
    }

    void IMoveable.Act()
    {
      Move();
    }
  }
like image 33
François Avatar answered Oct 05 '22 19:10

François