What do the angle brackets after the class name mean in a variable declaration?

I've been programming in Delphi for a while, but I've never come across the syntax I found in a question here on SO. There was this syntax:

var Dic: TDictionary<Integer,string>;

      

I have never seen <type, type>

. What does it mean? When and where can it be used? I didn't find anything because Google omits characters like "<", ">".

+3


source to share


2 answers


This is the syntax used for generics. Generics allows you to define classes that are parameterized by type.



You can read all about it in the Delphi documentation . You can also find the Wikipedia page. It gives a broader overview of the concept of general programming.

+7


source


In many languages, usually this is mapping or template creation, Delphi calls these generics and an example of declaring them can be seen here

type
  TPair<Tkey,TValue> = class   // TKey and TValue are type parameters
    FKey: TKey;
    FValue: TValue;
    function GetValue: TValue;
  end;

function TPair<TKey,TValue>.GetValue: TValue;
begin
  Result := FValue;
end;

      



As your specific example, a dictionary is used that will map integers to strings.

+6


source







All Articles