Tài liệu Lesson13: Interfaces - Pdf 84

Using Excellent Tools to Write Web Applications Targeting
the .NET Common Language Runtime (CLR)
[Home] [Up] [Lesson01] [Lesson02] [Lesson03] [Lesson04] [Lesson05
[Lesson06] [Lesson07] [Lesson08] [Lesson09] [Lesson10] [Lesson11
[Lesson12] [Lesson13]

On sale Now! C#
Unleashed is an in-
depth guide for
intermediate to
advanced software
developers to learn
the C#
programming
language and
serve as a desktop
reference. .
.
.
.
.
.
.
.
.
.
The C# Station Tutorial
by Joe Mayo, 02/24/02

interface, it's 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
{
Page 1 of 3Tutorial
6/24/2002http://www.csharp-station.com/Tutorials/Lesson13.aspx
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.
Listing 13-2. Using an Interface: InterfaceImplementer.cs
class InterfaceImplementer : IMyInterface
{
static void Main()
{
InterfaceImplementer iImp = new InterfaceImplementer();
iImp.MethodToImplement();

{
void MethodToImplement();
}

class InterfaceImplementer : IMyInterface
{
static void Main()
{
InterfaceImplementer iImp = new InterfaceImplementer();
iImp.MethodToImplement();
iImp.ParentInterfaceMethod();
}

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


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status