| Author: Legend 29 Aug 2008 | Member Level: Silver | Rating: Points: 6 |
A delegate declaration defines a reference type that can be used to encapsulate a method with a specific signature. A delegate instance encapsulates a static or an instance method. Delegates are roughly similar to function pointers in C++; however, delegates are type-safe and secure.
This declaration takes the following form::
attributes (Optional) Additional declarative information. For more information on attributes and attribute classes, see 17. Attributes. modifiers (Optional) The allowed modifiers are new and the four access modifiers. result-type The result type, which matches the return type of the method. identifier The delegate name. formal-parameters (Optional) Parameter list. If a parameter is a pointer, the delegate must be declared with the unsafe modifier.
Example 1
// keyword_delegate.cs // delegate declaration delegate void MyDelegate(int i);
class Program { public static void Main() { TakesADelegate(new MyDelegate(DelegateFunction)); }
public static void TakesADelegate(MyDelegate SomeFunction) { SomeFunction(21); } public static void DelegateFunction(int i) { System.Console.WriteLine("Called by delegate with number: {0}.", i); } }
Output Called by delegate with number: 21.
|
| Author: Athira Appukuttan 29 Aug 2008 | Member Level: Diamond | Rating: Points: 6 |
Hi..
Delegates add a safety to the idea of traditional function pointers. The .NET framework has added the bonus of providing a type-safe mechanism called delegates. They follow a Trust-But-Verify model, with automatic verification of the signature by the compiler. Unlike function pointers however, delegates are object-oriented, type-safe, and secured. In short, a delegate is a data structure that refers to a static method or to an object instance, and an instance method of that object. When the delegate references an instance method, it stores not only a reference to the method entry point, but also a reference to the object instance for which to invoke the method.
For more..
http://www.codeproject.com/KB/vb/Delegate.aspxhttp://www.aspnettutorials.com/tutorials/advanced/delegate-aspnet2-csharp.aspx
|