Вы находитесь на странице: 1из 4

B E G I N N E R S C # T U T O R I A L

L E S S O N 1 3 - I N T E R F A C E S
P A G E 1 O F 4
C O P Y R I G H T 2 0 0 0 2 0 0 3 C # S T A T I O N , A L L R I G H T S R E S E R V E D
B E G I N N E R S S
C # T U T O R I A L
1 3 . I N T E R F A C E S
W R I T T E N B Y J O E M A Y O
J M A Y O @ C S H A R P S T A T I O N . C O M
U P D A T E D 2 4 / 0 2 / 0 2 , 1 2 / 0 3 / 0 3
C O N V E R T E D T O P D F B Y A S H W E A V E R 0 2 / 0 9 / 0 3
W W W . C S H A R P S T A T I O N . C O M
B E G I N N E R S C # T U T O R I A L
L E S S O N 1 3 - I N T E R F A C E S
P A G E 2 O F 4
C O P Y R I G H T 2 0 0 0 2 0 0 3 C # S T A T I O N , A L L R I G H T S R E S E R V E D
This lesson teaches C# Interfaces. Our objectives are as follows:
Understand the Purpose of Interfaces.
Define an Interface.
Use an Interface.
Implement Interface Inheritance.
An interface looks like a class, but has no implementation. The only thing it
contains are definitions of events, indexers, methods and/or
properties. The reason interfaces only provide definitions is because they
are inherited by classes and structs, which must provide an implementation
for each interface member defined.
So, what are interfaces good for if they don't implement
functionality? They're great for putting together plug-n-play like
architectures where components can be interchanged at will. Since all
interchangeable components implement the same interface, they can be
used without any extra programming. The interface forces each component
to expose specific public members that will be used in a certain way.
Because interfaces must be defined by inheriting classes and structs, they
define a contract. For instance, if class foo inherits from the IDisposable
interface, it is making a statement that it guarantees it has the Dispose()
method, which is the only member of the IDisposable interface. Any code
that wishes to use class foo may check to see if class foo inherits
IDisposable. When the answer is true, then the code knows that it can call
foo.Dispose(). Listing 13-1 shows how to define an interface:
Listing 13-1. Defining an Interface: MyInterface.cs
interface IMyInterface
{
void MethodToImplement();
}
Listing 13-1 shows defines an interface named IMyInterface. A common
naming convention is to prefix all interface names with a capital "I". This
interface has a single method named MethodToImplement(). This could
have been any type of method declaration with different parameters and
return types. I just chose to declare this method with no parameters and a
void return type to make the example easy. Notice that this method does
not have an implementation (instructions between curly braces - {}), but
instead ends with a semi-colon, ";". This is because the interface only
specifies the signature of methods that an inheriting class or struct must
implement. Listing 13-2 shows how this interface could be used.

B E G I N N E R S C # T U T O R I A L
L E S S O N 1 3 - I N T E R F A C E S
P A G E 3 O F 4
C O P Y R I G H T 2 0 0 0 2 0 0 3 C # S T A T I O N , A L L R I G H T S R E S E R V E D
Listing 13-2. Using an Interface: InterfaceImplementer.cs
class InterfaceImplementer : IMyInterface
{
static void Main()
{
InterfaceImplementer iImp = new InterfaceImplementer();
iImp.MethodToImplement();
}
public void MethodToImplement()
{
Console.WriteLine("MethodToImplement() called.");
}
}
The InterfaceImplementer class in Listing 13.2 implements the IMyInterface
interface. Indicating that a class inherits an interface is the same as
inheriting a class. In this case, the following syntax is used:
class InterfaceImplementer : IMyInterface
Now that this class inherits the IMyInterface interface, it must implement
its members. It does this by implementing the MethodToImplement()
method. Notice that this method implementation has the exact same
signature, parameters and method name, as defined in the IMyInterface
interface. Any difference will cause a compiler error. Interfaces may also
inherit other interfaces. Listing 13-3 shows how inherited interfaces are
implemented.
Listing 13-3. Interface Inheritance: InterfaceInheritance.cs
using System;

interface IParentInterface
{
void ParentInterfaceMethod();
}
interface IMyInterface : IParentInterface
{
void MethodToImplement();
}
class InterfaceImplementer : IMyInterface
{
static void Main()
{
InterfaceImplementer iImp = new InterfaceImplementer();
iImp.MethodToImplement();
iImp.ParentInterfaceMethod();
}
B E G I N N E R S C # T U T O R I A L
L E S S O N 1 3 - I N T E R F A C E S
P A G E 4 O F 4
C O P Y R I G H T 2 0 0 0 2 0 0 3 C # S T A T I O N , A L L R I G H T S R E S E R V E D
public void MethodToImplement()
{
Console.WriteLine("MethodToImplement() called.");
}
public void ParentInterfaceMethod()
{
Console.WriteLine("ParentInterfaceMethod() called.");
}
}
The code in listing 31.3 contains two interfaces: IMyInterface and the
interface it inherits, IParentInterface. When one interface inherits another,
any implementing class or struct must implement every interface member
in the entire inheritance chain. Since the InterfaceImplementer class in
Listing 13-3 inherits from IMyInterface, it also inherits
IParentInterface. Therefore, the InterfaceImplementer class must
implement the MethodToImplement() method specified in the IMyInterface
interface and the ParentInterfaceMethod() method specified in the
IParentInterface interface.
In summary, you now understand what interfaces are. You can implement
an interface and use it in a class. Inherfaces may also be inherited by other
interface. Any class or struct that inherits an interface must also
implement all members in the entire interface inheritance chain.

Вам также может понравиться