Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Nbsp;   A First Look at Delegates

The C runtime’s qsort function takes a pointer to a callback function to sort elements within an array. In Windows, callback functions are required for window procedures, hook procedures, asyn- chronous procedure calls, and more. In the .NET Framework, callback methods are used for a whole slew of things. For example, you can register callback methods to get a variety of notifications such as

unhandled exceptions, window state changes, menu item selections, file system changes, form control

events, and completed asynchronous operations.

 

In unmanaged C/C++, the address of a non-member function is just a memory address. This address doesn’t carry any additional information such as the number of parameters the function expects, the types of these parameters, the function’s return value type, and the function’s calling convention. In short, unmanaged C/C++ callback functions are not type-safe (although they are a very lightweight mechanism).


In the .NET Framework, callback functions are just as useful and pervasive as in unmanaged Windows programming. However, the .NET Framework provides a type-safe mechanism called delegates. I’ll start off the discussion of delegates by showing you how to use them. The following code demonstrates how to declare, create, and use delegates.

using System;

using System.Windows.Forms; using System.IO;

 

 

// Declare a delegate type; instances refer to a method that

// takes an Int32 parameter and returns void. internal delegate void Feedback(Int32 value);

 

 

public sealed class Program { public static void Main() {

StaticDelegateDemo(); InstanceDelegateDemo(); ChainDelegateDemo1(new Program()); ChainDelegateDemo2(new Program());

}

 

private static void StaticDelegateDemo() { Console.WriteLine("­­­­­ Static Delegate Demo ­­­­­"); Counter(1, 3, null);

Counter(1, 3, new Feedback(Program.FeedbackToConsole));

Counter(1, 3, new Feedback(FeedbackToMsgBox)); // "Program." is optional Console.WriteLine();

}

 

private static void InstanceDelegateDemo() { Console.WriteLine("­­­­­ Instance Delegate Demo ­­­­­"); Program p = new Program();

Counter(1, 3, new Feedback(p.FeedbackToFile));

 

Console.WriteLine();

}

 

private static void ChainDelegateDemo1(Program p) { Console.WriteLine("­­­­­ Chain Delegate Demo 1 ­­­­­"); Feedback fb1 = new Feedback(FeedbackToConsole); Feedback fb2 = new Feedback(FeedbackToMsgBox);

Feedback fb3 = new Feedback(p.FeedbackToFile);

 

Feedback fbChain = null;

fbChain = (Feedback) Delegate.Combine(fbChain, fb1); fbChain = (Feedback) Delegate.Combine(fbChain, fb2); fbChain = (Feedback) Delegate.Combine(fbChain, fb3); Counter(1, 2, fbChain);

 

Console.WriteLine(); fbChain = (Feedback)

Delegate.Remove(fbChain, new Feedback(FeedbackToMsgBox)); Counter(1, 2, fbChain);

}


private static void ChainDelegateDemo2(Program p) { Console.WriteLine("­­­­­ Chain Delegate Demo 2 ­­­­­"); Feedback fb1 = new Feedback(FeedbackToConsole); Feedback fb2 = new Feedback(FeedbackToMsgBox);



Feedback fb3 = new Feedback(p.FeedbackToFile);

 

Feedback fbChain = null; fbChain += fb1;

fbChain += fb2; fbChain += fb3;

Counter(1, 2, fbChain);

 

Console.WriteLine();

fbChain ­= new Feedback(FeedbackToMsgBox); Counter(1, 2, fbChain);

}

 

private static void Counter(Int32 from, Int32 to, Feedback fb) { for (Int32 val = from; val <= to; val++) {

// If any callbacks are specified, call them if (fb != null)

fb(val);

}

}

 

private static void FeedbackToConsole(Int32 value) { Console.WriteLine("Item=" + value);

}

 

private static void FeedbackToMsgBox(Int32 value) { MessageBox.Show("Item=" + value);

}

 

private void FeedbackToFile(Int32 value) {

using (StreamWriter sw = new StreamWriter("Status", true)) { sw.WriteLine("Item=" + value);

}

}

}

 

Now I’ll describe what this code is doing. At the top, notice the declaration of the internal delegate, Feedback. A delegate indicates the signature of a callback method. In this example, a Feedback del- egate identifies a method that takes one parameter (an Int32) and returns void. In a way, a delegate is very much like an unmanaged C/C++ typedef that represents the address of a function.

The Program class defines a private, static method named Counter. This method counts inte- gers from the from argument to the to argument. The Counter method also takes an fb, which is a reference to a Feedback delegate object. Counter iterates through all of the integers, and for each integer, if the fb variable is not null, the callback method (specified by the fb variable) is called.

This callback method is passed the value of the item being processed, the item number. The callback method can be designed and implemented to process each item in any manner deemed appropriate.



Date: 2016-03-03; view: 574


<== previous page | next page ==>
Nbsp;   Unsafe Array Access and Fixed-Size Array | Nbsp;   Using Delegates to Call Back Static Methods
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)