Anonymous delegates: efficient and expressive


by Larry O'Brien, 3 Leaf Solutions, LLC. Intel Corp.

Since its first release, the .NET Framework has provided delegates, object-oriented types corresponding to a particular method signature. For instance, the following represents a delegate identified as Foo that takes two integers as parameters and returns an integer when executed:



delegate int Foo(int x, int y);


Most .NET programmers quickly become familiar with delegates as event handlers in Windows* Forms programming (an event is actually a delegate that is constrained in certain ways, such as only being invocable by the type in which it is declared). Delegates have broad usefulness, any time a final strategy can be separated from general-purpose routines and the final form of the strategy deferred until a later time; event-handling is just the most obvious example. Delegates are an alternative to maintaining a reference to one or more objects that implement an interface with a single method, a structure that brings to mind such design patterns as Strategy, Observer and Command.

Anonymous functions support in-line specification
In C# 1.x, there is only one way to instantiate a delegate. You use a syntax that is much like instantiating an object, passing in to the delegate constructor the name of some previously defined method whose method signature corresponds to the delegate's method signature (including return type).



delegate int Foo(int x, int y);

class Listing1
static int Plus(int x, int y) return x + y; static string myField = "Hello"; public static void Main() Foo f = new Foo(Listing1.Plus); int result = f(2, 3); System.Console.WriteLine(result); string refToMyField = Listing1.myField; System.Console.WriteLine(refToMyField); string variable = "world!"; System.Console.WriteLine(variable);

In Listing 1, the Foo delegate is instantiated with a reference to the static method Listing1.Plus(int, int) in a manner analogous to the way that the refToMyField object is instantiated with a reference to the static field myField. In C# 1.x, there is no delegate capability similar to the ability of variable to be assigned a value from an in-line specification. C# 2.0, however, supports the following:

Subscribers who liked this article also read:
Getting the bubbles out of code: designing for the Itanium 2 processor
by Andrew Binstock, principal analyst, Pacific Data Works LLC. Intel C...

If you're interested in this topic, these articles may be helpful:

Intel® Math Kernel Library for Windows
The flagship for HPC software, Intel® MKL is a set of highly optim...
Integrating Java and Microsoft .NET
by Jon Jagger, Content Master Ltd. Intel Corp. The right techniques...
Data access performance in ADO.NET
by Ramesh Theivendran, architect, Borland Software Corp. First publish...
.NET client application "no touch" deployment: harness the full processing power of the desktop
by Christopher Goldfarb, senior software architect, Intel Software Sol...
Java Graph library v 2.1 demo
from Big Faceless Organization The Big Faceless Java Graph library ...

Related Jobs:

Computer Forensics Specialist #2007RSA-TJFTH173914IL33 - VA - Lorton - Computer Sciences Corporation
Principal Duties and Essential Job Functions: Computer Forensics Exam...
Seinor Developer #3319 - MD - Columbia - Vision Technology
Position Description- Design and develop HTML for all pages using Fron...
Solutions Architect #DEE00027 - IL - Deerfield - Marsh Inc.
Solutions Architect DEE00027 (To return to the Job List,...
Systems Development Specialist, Principal #18529 - MD - Patuxent River - CACI International Inc
Requirements: CACI has an immediate opening for a Principle Systems D...
Software Developer #3309 - AZ - Tuscon - Affiliated Computer Services, Inc.
Affiliated Computer Services, Inc., ACS, is a global FORTUNE 500 comp...
Production Support Manager #24141 - NY - New York - Morgan Stanley
Production Support Manager Date Posted: 8/24/2007 Functional ...
Software Engineer, Staff #6249 - NC - Durham - Synopsys, Inc.
US54-Durham - R&D Engineering -----------------------------------...
Oracle DBA #SUORACL - ND - Fargo - UPP Business Systems
Description: Oracle DBA possessing in-depth understanding of all aspec...
Multiple .NET Developers #MHMULT - MI - Midland - UPP Business Systems
Multiple .NET Developers Technology: Web/E-Commerce/.NET Descript...
Information Technology Associate #23982 - NY - New York - Morgan Stanley
Information Technology Associate Date Posted: 8/20/2007 Funct...