Name
Nil Keyword
Syntax
const nil = Pointer(0);
Description
The nil keyword is a special
Pointer value that is guaranteed to be distinct
from any real
pointer.
The numeric value of nil is zero. When you create
a new object, therefore, and Delphi initializes all the
object’s fields to zero, you can rely on pointer, class,
interface, and method type fields to be initialized to
nil.
Tips and Tricks
You can also assign
nilto method-type variables or fields, in which case Delphi storesnilfor the code and data pointers. When comparing a method withnil, Delphi checks only the code pointer. In other words, the following two examples are equivalent:
if Notify = nil then ... if (TMethod(Notify).Code = nil then ...
The most common use for
nilis to mark pointer and method-type variables with a “not-a-pointer” value. For example, an event property that does not have an event handler assigned to it has the valuenil.Assigning
nilto an interface-type variable causes Delphi to call_Releaseon the old value of the variable (if the variable was notnilto start with).Delphi represents an empty dynamic array, long string, or wide string as a
nilpointer.Many Delphi programmers prefer the
Assignedfunction over a comparison withnil. It is primarily a style issue, but see theAssignedfunction for more information.
See Also
| Assigned Function, Pointer Type |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access