Posts

Showing posts with the label TObjectList<T>

Generic Types and Classes in Delphi

Generic Classes in Delphi Generics are powerful language features that allows to write type-safe classes, methods that act upon a type to be provided later. Generic classes makes it possible to bind a specific type with a class that means we can instantiate a class tied to a given data type. This makes code type safer. We can declare  generic type to accept any type, or we can use constraints, which will limit the types that can be passed to ones with parameterless constructors, or types of a specific class or those that implement specific interfaces. Once we define a specific type on generic declaration, this is enforced by the compiler that we cannot assign a different type value to the class member. Generic classes are as efficient as normal classes but in some case it is more efficient than other classes as the need for runtime type cast is reduced. Generally we use Generics  on cases when we need lots of type casting with a specific type or we are writing a lots of su...