How to initialize TList <T> in one step using Delphi?
I am sure this is a simple question, but I cannot get it to work:
var FMyList: TList<String>; begin FMyList := TList<String>.Create(?????); end; How to insert instead ????? to insert these three lines:
'one'
'two'
'three'
Thanks..
+7
ferpega
source share2 answers
There is no single method for this. You can write your own constructor to do it like this:
constructor TMyList<T>.Create(const Values: array of T); var Value: T; begin inherited Create; for Value in Values do Add(Value); end; Then you could write:
FList := TMyList<string>.Create(['one', 'two', 'three']); Update
As Uwe correctly points out in his answer, the code I submitted should use the AddRange() method:
constructor TMyList<T>.Create(const Values: array of T); begin inherited Create; AddRange(Values); end; +5
David heffernan
source shareNot one liner, but two liners:
FMyList := TList<String>.Create; FMyList.AddRange(['one', 'two', 'three']); Edit: Of course, you can combine it with David's approach.
+15
Uwe raabe
source share