Generic Methods

From The Oxygene Language Wiki

Jump to:navigation, search

This is a Language topic
Feel free to add your notes to this topic below.



A generic method is a method with one or more generic type parameters. It can be defined by placing a generic definition after the name of the method, like:

type
  MyClass = class
    method DoSomething<T>(Data: T); where T is IEnumerable;
  end;

Here, the DoSomething method is defined with a single generic parameter called T that has to implement the IEnumerable interface. Inside the DoSomething function T can be used like any other type that implements IEnumerable.

Calling a generic method can be done with and without specifying the generic parameter type. When the type is omitted the type is inferred from the parameter passed.

var
  Data: array of String := ['a', 'b', 'c'];
  aClass: MyClass;
begin
  aClass.DoSomething<array of string>(Data);

  aClass.DoSomething(Data); // same as above


See Also


Oxygene-48.png

Area: Oxygene Language
Compiler version: Oxygene 5

Language GlossaryKeywordsTypesFAQHow To

Navigation
Areas
More
Toolbox