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?
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.
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 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.
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.
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.
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.
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).
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With