Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is C#'s most effective counterpart to Delphi's TStringList?

In our Delphi application, we use a TStringList to store strings and corresponding objects. For another project, I need to do something similar to this in C#, but am not sure what the most effective way of doing this is. So far I've thought of using an array list, a list, or a dictionary. Would one of these be effective at what I want to do? If not, which is a good way to go?

like image 456
Tom A Avatar asked Dec 12 '22 16:12

Tom A


1 Answers

It depends what features of TStringList you need. There's not really a direct replacement.

A dictionary<string,object> is unordered, and you cannot have duplicate strings. There's no Text property to set all strings at once, etc. If all that is OK with you, I'd go for that.

Otherwise, you might consider defining a little class like:

public class Item { 
  public string String {get;set;} 
  public object Object {get;set;}
}

and then use List<Item>. That gives you an ordered list of (string,object) tuples.

like image 129
Blorgbeard Avatar answered Mar 15 '23 01:03

Blorgbeard