Click here to Skip to main content
13,557,707 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 30 Dec 2012
Licenced CPOL

Anonymous Methods in Delphi

, 30 Dec 2012
Rate this:
Please Sign up or sign in to vote.
An introduction to anonymous methods in Delphi


Under the scope of Delphi, an anonymous method is either a procedure or function that’s unattached to an identifier. In other words, anonymous methods don’t have names, which is why they are called “anonymous”.

Basically, you can assign a block of code (in the form of a procedure or function) directly to a variable.

I am going to give you a simplistic example. I am going to Keep it simple, Stupid! to avoid distracting you with any complexity.

This is the wording of the task: create a console application which prints “Hello world” and “Good bye” to the standard output. Constraint: Use the Writeln function just once in the code.

To accomplish such reckless task in the old days (before the introduction of anonymous methods), you could do this:

program Project1;


procedure PrintString(aText: string);

  PrintString('Hello world');
  PrintString('Good bye');

How to do the same with anonymous methods? Take a look at the following code:


  TMyAnonymousProcedureType = reference to procedure(aText: string);

  A: TMyAnonymousProcedureType;

  A:= procedure(aText: string) //No semicolon here
  A('Hello world');
  A('Good bye');

As you can see in the example above, we have assigned code directly to the variable A. Then, we called A with parameters, and voilà!: we have accomplished our reckless task with anonymous methods as well.

Pay attention to this: if you are tented to declare variable A like this:

  A: reference to procedure(aText: string);

Don’t! That shortcut doesn’t work. You’ll get a compilation error… like this:

Undeclared identifier: ‘reference’

So, you do need to declare:
  TMyAnonymousProcedureType = reference to procedure(aText: string);

Only later, you can define the type of variable A.

You might be asking by now: why to bother with all this? What's the benefit? Well, in the previous example there’s little or no benefit present.

Generally speaking, anonymous methods are handy in the following cases:

  • You have been trying to name a local method for hours. You cannot think of a name for it. Well, think no more: use anonymous methods. Don’t put a tasteless name like Foo(), XXX(), Aux(), etc.
  • You create a function that is called just once (it’s just called from one spot).
  • You can use anonymous methods to provide elegant and simpler implementations. This is the case when combining generics types with anonymous methods for example. I should write about this shortly. Subscribe to my feed and stay tuned Smile | :)

With this post, I wanted to introduce anonymous methods to you. It’s OK if you don’t see the benefits clearly right now. You’ll get there. Smile | :)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer Digital Rapids
Canada Canada
My name is Yanniel Alvarez Alfonso. I was born in San Antonio de los Baños, Havana Province, Cuba on October 24th, 1982.

I majored in Information Technology Engineering at José Antonio Echeverría Polytechnic Institute (CUJAE) in Havana City, Cuba (July 2006). After that, I got a Masters Degree in Applied Computer Science at the same University (May 2009).

I used to work as a professor of Information Technology at CUJAE. Right now, I work as a Software Developer in Toronto, Canada. I moved to Canada under the Skilled Worker Program on February 26th, 2010.

This is my personal blog: Yanniel's notes; in which I write about miscellaneous topics.

The link at the end of this sentence compiles an index of all the articles I have written so far about Delphi Programming.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180515.1 | Last Updated 31 Dec 2012
Article Copyright 2012 by yanniel
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid