Search code examples
delphicollectionsdelphi-xe2spring4d

How do I create a case insensitive list of string?


I'm trying to create a list of string that is case-insensitive.

The CreateList method let's me pass in some overloads:

  • TComparison<System.string>
  • IComparer<System.string>

I tried to use TStringComparer.OrdinalIgnoreCase like this:

var
  List: IList<string>;
begin
  List := TCollections.CreateList<string>(TStringComparer.OrdinalIgnoreCase);
end;

But since this comparer doesn't implement any of the above classes / interfaces that doesn't compile; I get:

E2250 There is no overloaded version of TCollections.CreateList<System.string> that can be called with these arguments

Is there an implementation of one of those available in the spring4d framework?


Solution

  • You need to write the parentheses:

    var
      List: IList<string>;
    begin
      List := TCollections.CreateList<string>(TStringComparer.OrdinalIgnoreCase());
    end;
    

    Later compiler versions can figure it out without.