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

C#

ENTER DELEGATES

Delegates act like pointers that you find in other languages such as C++, but delegates go several steps further in C#. Delegates provide object-

oriented pointers to methods from other points in your project. This approach makes it easy for methods throughout your program to retrieve information from one source without having to enter that information repeatedly.

Delegates provide two key benefits. Delegates act as a central point where all pieces of your code that need objects refer to a specific method. It is quite inconvenient to have to write static methods for many different classes.

It is also inconvenient to refer to the same class for the same method, and that approach can also slow your project down when it runs. It is much more efficient to have one or a few delegates that can handle method operations for your entire project.

The second benefit of delegates is anonymity. The delegate does not care about what the method includes — whether it be static or non-static, what accessibility the method has, or any other information. The only thing the delegate cares about is if the method that it is looking for has the same signature as the delegate.

ENTER DELEGATES

Visual C# Projects

Console

Applications

 

The Start page appears.

The New Project

 

 

¤ Click New Project.

window appears.

 

 

 

Click the Console

 

 

 

 

 

 

 

 

 

 

 

Application icon in the

 

 

 

Templates pane.

Type a name for the file.

ˇ Click OK.

PROGRAMMING METHODS AND EVENTS 6

You can combine existing delegates into a multi-cast delegate.

TYPE THIS:

using System;

delegate void Del1(string x) class Class1

{

public static void On(string x)

{

Console.WriteLine("Switch goes on.", x);

}

public static void Off(string x)

{

Console.WriteLine("Switch goes off.", x);

}

public static void Main();

{

Del1 a, b, c;

a = new Del1(On); b = new Del1(Off); c = a + b;

Console.WriteLine("The two switch states:"); c;

}

}

RESULT:

The two switch states:

Switch goes on.

Switch goes off.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Á Type the Main method

 

 

 

 

 

Type the code for the

 

 

code that outputs the values.

methods that specify and

 

 

 

output the delegate value.

° Press the F5 key.

· Save the program as the

The delegate output appears.

filename.

 

131

C#

PROGRAM EVENTS

In object-oriented programming, an event lets clients of that class — clients can include delegates, other classes, methods, and indexers — know about something that

happens to an object in a class. That something that happens is of great interest to the clients in the class and so the event lets those clients know about it and act accordingly.

By acting accordingly, the clients give the class delegates so the delegates can retrain those objects using the modules called by those delegates. Once the appropriate module retrains the changed object to behave properly, the object goes back to the class for further processing.

When you declare an event inside of a class you must declare the delegate inside the event. The class that the event resides in is the only class that calls the event. When the class calls the event, the class checks to see if a client has hooked up a delegate to the event, and if that is true then the class processes the event.

The previous task mentioned that C# declares events using delegates. If you have come to this task wanting to learn about events but you have not learned about delegates yet, skip back four pages and read about delegates before you continue on.

PROGRAM EVENTS

Console

Applications

The New Project

NET

 

window appears.

 

 

Click the Console

 

 

 

 

Application icon in the

.

 

Templates pane.

 

 

 

Type a name for the file.

 

 

 

 

 

 

 

ˇ Click OK.

 

 

 

 

Á Type the code that establishes your event delegate, interface, and class.

PROGRAMMING METHODS AND EVENTS 6

The facts that events can only be called from the classes they reside in and that classes can be inherited poses an interesting problem. C# does not let you invoke events in a base class from an inheriting class. This seems to defeat the purpose of having a class inherit all information from your base class. However, C# does offer a workaround to this problem.

C# can have an inheriting class call a base class event by creating a protected invoking method within the event. This method invokes the base class event and the project passes along the information from that base class event to the rest of the event. If you would rather not have the base class send its events, you can have this protected invoking method as a virtual method. An overriding method in an inheriting class can then take over from the virtual method and shut down the base class events.

Type the code for the Output class that outputs the string when the event fires.

° Press the F5 key.

· Save the program as the

The string appears on the

filename.

 

screen when the event fires.

 

133

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