Skip to main content
infervour.com

Back to all posts

How to Extend A Generic Collection In Delphi?

Published on
6 min read
How to Extend A Generic Collection In Delphi? image

Best Delphi Programming Books to Buy in October 2025

1 The Little Book Of Delphi Programming: Learn To Program with Object Pascal (Little Programming Books)

The Little Book Of Delphi Programming: Learn To Program with Object Pascal (Little Programming Books)

BUY & SAVE
$18.99
The Little Book Of Delphi Programming: Learn To Program with Object Pascal (Little Programming Books)
2 Delphi, in all its glory: …For more than mere programmers

Delphi, in all its glory: …For more than mere programmers

BUY & SAVE
$34.99
Delphi, in all its glory: …For more than mere programmers
3 Coding in Delphi

Coding in Delphi

BUY & SAVE
$14.99
Coding in Delphi
4 Expert Delphi: Robust and fast cross-platform application development

Expert Delphi: Robust and fast cross-platform application development

BUY & SAVE
$45.99
Expert Delphi: Robust and fast cross-platform application development
5 Modern Application Development with Delphi (Color Print): Learn to develop a desktop application, plus an API and an ORM

Modern Application Development with Delphi (Color Print): Learn to develop a desktop application, plus an API and an ORM

BUY & SAVE
$64.99
Modern Application Development with Delphi (Color Print): Learn to develop a desktop application, plus an API and an ORM
6 Delphi High Performance.: Master the art of concurrency, parallel programming, and memory management to build fast Delphi apps

Delphi High Performance.: Master the art of concurrency, parallel programming, and memory management to build fast Delphi apps

BUY & SAVE
$39.99
Delphi High Performance.: Master the art of concurrency, parallel programming, and memory management to build fast Delphi apps
7 More Coding in Delphi

More Coding in Delphi

BUY & SAVE
$14.99
More Coding in Delphi
8 Delphi Pascal Programming: Efficient Code Editing, Visual Designing, And Integrated Debugging

Delphi Pascal Programming: Efficient Code Editing, Visual Designing, And Integrated Debugging

BUY & SAVE
$26.85 $39.95
Save 33%
Delphi Pascal Programming: Efficient Code Editing, Visual Designing, And Integrated Debugging
9 Mastering Delphi Programming: A Complete Reference Guide: Learn all about building fast, scalable, and high performing applications with Delphi

Mastering Delphi Programming: A Complete Reference Guide: Learn all about building fast, scalable, and high performing applications with Delphi

BUY & SAVE
$43.13 $46.99
Save 8%
Mastering Delphi Programming: A Complete Reference Guide: Learn all about building fast, scalable, and high performing applications with Delphi
10 Object Pascal Handbook Delphi 10.4 Sydney Edition: The Complete Guide to the Object Pascal programming language for Delphi 10.4 Sydney

Object Pascal Handbook Delphi 10.4 Sydney Edition: The Complete Guide to the Object Pascal programming language for Delphi 10.4 Sydney

BUY & SAVE
$48.50
Object Pascal Handbook Delphi 10.4 Sydney Edition: The Complete Guide to the Object Pascal programming language for Delphi 10.4 Sydney
+
ONE MORE?

To extend a generic collection in Delphi, you can follow the steps outlined below:

  1. Create a new unit file (.pas) to define your extended collection class.
  2. Begin by declaring your new class, which should extend the built-in generic collection type that you want to extend. For example, if you want to extend the TList class, declare your class as follows:

type TExtendedList = class(TList) // your code here end;

  1. Within your class declaration, you can add new methods, properties, or any additional functionality you desire. You can override existing methods to modify their behavior or implement entirely new methods based on your requirements.
  2. To use your extended collection, make sure the unit containing your new class is included in the uses clause of the unit in which you want to use it.

Here's a complete example showing how to extend the TList<T> class by adding a new method:

unit ExtendedList;

interface

uses System.Generics.Collections;

type TExtendedList = class(TList) procedure PrintItems; end;

implementation

procedure TExtendedList.PrintItems; var Item: T; begin for Item in Self do Writeln(Item.ToString); end;

end.

In this example, the TExtendedList class extends the TList<T> class and adds a PrintItems method. This method iterates over the elements in the list and prints each item using the ToString method.

You can then use the extended collection in your application as follows:

uses ExtendedList;

var MyList: TExtendedList; begin MyList := TExtendedList.Create; MyList.Add(1); MyList.Add(2); MyList.Add(3);

MyList.PrintItems; // This will print all items in the list end.

That's it! You have successfully extended a generic collection in Delphi. Remember to adjust the code as per your specific collection type and desired functionality.

How to reverse the order of elements in a generic collection in Delphi?

To reverse the order of elements in a generic collection in Delphi, you can use the TList<T> class from the System.Generics.Collections unit.

Here is an example code that demonstrates how to reverse the order of a TList<T> collection:

uses System.Generics.Collections;

var MyList: TList; i: Integer; begin // Create and populate the list with some elements MyList := TList.Create; MyList.AddRange([1, 2, 3, 4, 5]);

// Display the original order for i := 0 to MyList.Count - 1 do Write(MyList[i], ' '); WriteLn;

// Reverse the order of the elements MyList.Reverse;

// Display the reversed order for i := 0 to MyList.Count - 1 do Write(MyList[i], ' '); WriteLn;

MyList.Free; end.

Output:

1 2 3 4 5 5 4 3 2 1

In this example, the TList<Integer> collection is created and populated with some integer values. The Reverse method is then called on the list to reverse the order of the elements. Finally, the reversed order is displayed using a loop.

You can replace Integer with any other data type or class in the code example to reverse the order of a collection of that specific type.

How to add elements to a generic collection in Delphi?

To add elements to a generic collection in Delphi, you can follow these steps:

  1. Declare a variable of the generic collection type you want to use. For example, if you want to use a generic TList collection, declare a variable of type TList. var myList: TList;
  2. Create an instance of the generic collection using the TList.Create constructor. This will initialize the collection. myList := TList.Create;
  3. Use the Add method of the generic collection to add elements. Pass the value you want to add as a parameter to the Add method. myList.Add(1); myList.Add(2); myList.Add(3); You can add elements of any compatible type to the generic collection. In this example, we are adding Integer values.
  4. When you are finished adding elements, make sure to free the memory used by the collection using the Free method. myList.Free;

Here is a complete example that demonstrates adding elements to a generic TList collection:

program GenericCollectionExample;

uses System.Generics.Collections;

var myList: TList; i: Integer; begin myList := TList.Create;

myList.Add(1); myList.Add(2); myList.Add(3);

for i in myList do Writeln(i);

myList.Free; end.

When you run this program, it will output the following:

1 2 3

Note that the generic collections in Delphi provide many other methods and properties for manipulating and accessing elements. You can refer to the documentation and explore those features based on your specific requirements.

How to check if a specific element exists in a generic collection in Delphi?

In Delphi, you can use the Generics.Collections unit to work with generic collections. To check if a specific element exists in a generic collection, you can use the IndexOf method.

Here's an example code that demonstrates how to check if a specific element exists in a generic collection:

uses Generics.Collections;

// Helper function to check if a specific element exists in a generic collection function ElementExists(const Collection: TList; const Element: T): Boolean; begin Result := Collection.IndexOf(Element) > -1; end;

var MyList: TList; Element: Integer; begin // Create a generic list and populate with some elements MyList := TList.Create; MyList.Add(1); MyList.Add(2); MyList.Add(3);

// Specify the element to check Element := 2;

// Check if the specified element exists in the list if ElementExists(MyList, Element) then WriteLn('The element exists in the list.') else WriteLn('The element does not exist in the list.');

// Free the memory used by the list MyList.Free; end.

In this example, the ElementExists function accepts a generic TList<T> collection and an element of type T. It uses the IndexOf method of the collection to check if the element exists. If the index is greater than -1, it means the element is found, and the function returns True, otherwise it returns False.

How to access the elements of a generic collection in Delphi?

To access the elements of a generic collection in Delphi, you can use the for..in loop or the Items property.

  1. Using the for..in loop:

program AccessGenericCollection;

uses System.Generics.Collections;

var myCollection: TList; element: Integer;

begin myCollection := TList.Create;

myCollection.Add(10); myCollection.Add(20); myCollection.Add(30);

// Access elements using the for..in loop for element in myCollection do Writeln(element);

myCollection.Free; end.

  1. Using the Items property:

program AccessGenericCollection;

uses System.Generics.Collections;

var myCollection: TList; i: Integer;

begin myCollection := TList.Create;

myCollection.Add(10); myCollection.Add(20); myCollection.Add(30);

// Access elements using the Items property for i := 0 to myCollection.Count - 1 do Writeln(myCollection.Items[i]);

myCollection.Free; end.

Both methods will allow you to access and process each element of the generic collection.

How to check if a generic collection is empty in Delphi?

To check if a generic collection is empty in Delphi, you can use the Count property of the collection. If the Count property is equal to 0, then the collection is empty.

Here is an example:

uses System.Generics.Collections;

var MyList: TList; begin MyList := TList.Create;

// Check if the collection is empty if MyList.Count = 0 then WriteLn('The collection is empty.'); // Output: The collection is empty.

MyList.Free; end.

In this example, a TList<Integer> collection is created using the TList class from the System.Generics.Collections unit. The Count property is then used to check if the collection is empty. If the Count property returns 0, the message "The collection is empty." is displayed.