Delegate

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
Exam Prep. Guides
Exam 70-536 Study Guide

1. Types and collections

2. Process, threading,…
3. Embedding features
4. Serialization, I/O
5. .NET Security
6. Interop., reflection,…
7. Global., drawing, text

edit

Summary

A delegate is basically a reference to a method. A delegate can be passed like any other variable. This allows the method to be called anonymously, without calling the method directly.

In C#, delegates are the basis for events.

For example:

A object can create a delegate that refers to one of its private methods. Then the object can pass that delegate to another object. The second object could then call that private method without knowing anything about it (loose-binding).

Code Examples

These examples demonstrate delegates that are not used for events

Naming convention used in these Code Examples:

Name delegate types with a suffix of "Callback"

Name delegate instances with a previx of "do"

do stands for both "do something" and "delegate object"


Example Names:

// Delegate Declaration
public delegate void AcceptCallback();
 
// Delegate Instance 
AcceptCallback doAccept;


Declare a void delegate that takes no parameters:

public delegate void ExecuteCallback();

Declare a delegate that has parameters and returns a value:

public delegate int AccountComparisonCallback( Account a, Account b );

Declare a generic delegate that has parameters and returns a value:

public delegate int CompareCallback<T>( T a, T b );

Create a delegate object that refers to a method called Compare:

AccountComparisonCallback doComparison = new AccountComparisonCallback( Compare );

Call a delegate

// (Outside the class)
// The delegate type declaration
public delegate int AccountComparisonCallback( Account a, Account b );
 
// (Inside the class)
// The delegate instance
private AccountComparisonCallback _doComparison;
 
// This method calls the comparison delegate safely
// Which simplifies the call so other class methods 
// can easily call the delegate
private int CallComparison( Account a, Account b )
{
	// Check for null before calling a delegate
	if( _doComparison != null )
	{
		// Call the delegate like a method
		return _doComparison( a, b );
	}
		// If the delegate is null, return 0
	else
	{
		return 0;
	}
}


Create a delegate object and pass it as a parameter

// The AccountComparison Delegate
public delegate int AccountComparisonCallback( Account a, Account b );
 
public class AccountSorter
{
	// Declare a method that has the same signature as the delegate
	private int Compare( Account a, Account b )
	{
		// ...
	}
	
	// Declare a method that uses the delegate
	public void Sort( List<Account> accounts )
	{
		// Create the delegate object that refers to the Compare method
		AccountComparisonCallback doComparison = new AccountComparisonCallback( Compare );
		
		// Pass the delegate as an argument to a method
		accounts.Sort( doComparison );
		
		// Explaination:
		// Inside the Sort method, the delegate will be called
		// which calls the private Compare method to which it refers
		// In this way the Sort method is able to call a method without 
		// knowing anything about the actual method
	}
}

See Also


Today's Deals: Electronics

Personal tools