September 22, 2011
interface to call methods Detailed

basics of the interface definition:
interface is a named method signature. So the interface where you can define methods, properties, events, since these are essentially methods. However, the interface does not define any constructors.
interface accessibility:
class itself modifier for public, internal and other (VS2008 where tried). But the display of an interface method, the interface members are not allowed to add any modifiers (VS2008 where tried).

interface inheritance and call
implicitly implements an interface method:
example: < br />
define a class Base, which implements the IDisposable / / This class is derived from Object and it implements IDisposable
internal class Base: IDisposable
{
/ / This method is implicitly sealed and cannot be overridden
publicvoidDispose ()
{
Console. WriteLine (“Base Dispose”);
}
}
/ / This class is derived from Base and it re-implements IDisposable
internal class Derived: Base, IDisposable
{
/ / This method cannot override Base Dispose. ew is used to indicate
/ / that this method re-implements IDisposable Dispose method
newpublicvoidDispose ()
{
Console.WriteLine (“Derived Dispose “);
/ / NOTE: The next line shows how to call a base class implementation (if desired)
/ / base.Dispose ();
}
call results show:
public static void Main ()
{
Baseb = newBase ();
/ / use the type of b to call Dispose: results of Base Dispose
b.Dispose ();
/ / used b the type of the object pointed to call Dispose,
/ / b is the type of object because the Dispose method of IDisposable object is achieved b
/ / so the result is ; Base Dispose, and above the same
((IDisposable) b). Dispose ();
Derivedd = newDerived ();
/ / d of the type used to call Dispose, the results; Derived Dispose
d.Dispose ();
/ / the object pointed to by d type to call Dispose, the results: “Derived Dispose”
((IDisposable) d). Dispose ();
b = newDerived ();
/ / use b, type to call Dispose, b is of type Base. The result is: Base Dispose
b.Dispose ();
/ / with b referring to the type of object called Dispose, b refers to the object is Derived, so the result is : Derived Dispose
((IDisposable) b). Dispose ();
}
above the interface method call, pay attention to two Concept: When you call an interface method, in the end is the object type referred to in the call, or type in the call.
above the Base class and Derived class Dispose method implicitly implements IDisposable interface, the Dispose method. If the IDisposable interface, the display of the Dispose method to do, you can more clearly see this.
now show the code to do the implementation.
internal class Base: IDisposable
{
/ / This method is implicitly sealed and cannot be overridden
publicvoidDispose ()
{
Console.WriteLine (“Base Dispose”);
}
/ / / < summary>
/ / / Base display of IDisposable
/ / /
voidIDisposable.Dispose ()
< br /> {
Console.WriteLine (“Base-> IDisposable.Dispose”);
}
}
internalclassDerived: Base, IDisposable
{
/ / This method cannot override Base Dispose. ew is used to indicate
/ / that this method re-implements IDisposable Dispose method
newpublicvoidDispose ()
{
Console.WriteLine (“Derived Dispose”);

/ / NOTE: The next line shows how to call a base class implementation (if desired)
/ / base.Dispose ();
} < br />
/ / /


/ / / display of the Dispose method
/ / /

voidIDisposable.Dispose ()
{
Console.WriteLine (“Derived-IDisposable.Dispose”);
}
< br />}
same calling code is as follows, the result is:
public static void Main ()
{
< br /> Baseb = newBase ();
/ / use the type of b to call Dispose, b is of type Base, the result type is called a public method public voidDispose (): result of Base Dispose
b.Dispose ();
/ / with b referring to the type of object to call Dispose,
/ / so the result is; Base- > IDisposable.Dispose, and the above is not the same.
((IDisposable) b). Dispose ();
Derivedd = newDerived ();
/ / d of the type used to call Dispose , d is of type Derived, it calls the method publicvoidDispose (). The result is Derived Dispose
d.Dispose ();
/ / type of object referred to by d to call Dispose, d object type is shown converted IDisposable, so It calls
b = newDerived ();
/ / the type of call with b Dispose, b is of type Base. The result is: Base Dispose
b.Dispose ();
/ / object with b referring to the type of call Dispose, b refers to the object is Derived, Derived Types The object is cast into the interface type IDisposable.
/ / it calls the voidIDisposable.Dispose (). So the result is: “Derived-IDisposable.Dispose”
((IDisposable) b). Dispose ();
}