Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Hungry Minds - Visual C# Blueprint.pdf
Скачиваний:
101
Добавлен:
12.02.2016
Размер:
9.71 Mб
Скачать

C#

ENTER REFERENCE TYPE DECLARATIONS

Visual C# offers three different keywords for declaring reference types: class, interface, and delegate. The class, interface, and delegate types have

similar statement structures. They include optional class attributes and modifiers that further define your reference type and the identifier, which is the name of your reference type. After that the options change depending on the reference type you use. For example, with classes, you have the ability to specify a base class and any class member declarations. An interface and a class are also closely related in that they can rely on base versions of themselves that contain basic data but no members.

A class contains references about data. In contrast, an interface contains references about how that data should be used — that is, what methods, properties, events, and indexers should apply to that data. Interfaces contain only abstract members that have basic information about how data in a class or struct should behave.

Classes and structs can apply to more than one interface, and the class and/or struct must adhere to that interface much like you must adhere to a contract that you sign.

ENTER REFERENCE TYPE DECLARATIONS

Properties

Project. The New Project window appears.

Click the Console Application icon in the Templates pane.

Type a name for the file.

ˇ Click OK.

WORKING WITH TYPES AND INTERFACES 5

To save keystrokes, you can implement an interface directly from a class.

TYPE THIS:

using System; interface IntBase1

{

void IBMethod1();

}

interface IntBase2

{

void IBMethod2();

}

interface Int1: IntBase1, IntBase2

{

void Method1();void Method2();

}

interface Int2: IntBase1, IntBase2

{

void Method3(); void Method4(); void Method5();

}

class Class1: Int1, Int2

{

public static void Main()

{

Console.WriteLine("This class inherits from two interfaces that inherit from two base interfaces. No values are returned because all the interfaces do are return void methods.");

}

RESULT:

This class inherits from two interfaces that inherit from two base interfaces. No values are returned because all the interfaces return void methods.

The appears window

Á Delete

CONTINUED

within

 

C#

ENTER REFERENCE TYPE DECLARATIONS

he delegate reference type serves two functions. First, Ta delegate object serves as the primary object in an

event. An event tells your project about something that happens to an object in your program. Second, the delegate object contains method information that tells the affected object in the event what to do when the event occurs.

Delegates act like function pointers in other languages such as C++ and Pascal. Unlike other languages, Visual C# delegates are completely object-oriented so they are secure and type-safe. Type-safe code is code that accesses types in well-defined ways so as to prevent crashing programs that

can lead to other nasty things such as memory leaks and crashing operating systems.

When you create a delegate, you must enter two mandatory options. First, you must enter the result type that matches the return type of the method. Entering the result type lets you tie in the delegate with the method. Second, you must enter the delegate name. Without either of those options, the MDE window calls your attention to the error. You can add attributes and modifiers as you can with classes and interfaces.

ENTER REFERENCE TYPE DECLARATIONS (CONTINUED)

The New Project window appears.

¡ Click the Console Application icon in the Templates pane.

Type a name for the file.

£ Click OK.

WORKING WITH TYPES AND INTERFACES 5

No matter if you write your delegate before or after you write your method, avoid compilation errors by ensuring that the delegate result type and your method return type match before you compile your project.

The greatest similarity between delegates and interfaces is that they separate the specification of methods with the implementation of those methods. As with the class and struct, your decision about using a delegate or an interface depends on what you are trying to do.

If you need to call a single method or you want a class to refer to several methods, use the delegate. The delegate also has the added advantage of being easier to construct than the interface. However, the interface lets you specify the methods that an object in your project calls instead of general methods that a delegate includes. The interface is also a good choice if a class needs an inheriting interface as a jump point for accessing other interfaces or classes.

 

 

 

 

 

The Class1.cs code

 

 

 

Type the code that

appears in the parent

establishes the delegate, calls

window.

the delegate, and outputs the

¢ Delete the comments

result.

 

 

 

within the Main method.

 

 

 

§ Run the program by

Save the program as the

pressing the F5 key.

filename.

The constant expression

 

appears onscreen.

 

103

Соседние файлы в папке c#