Delphi 的 TList 在 C# 中的等价物是什么?

问题描述 投票:0回答:2

在Delphi编程中,我有一个类并将该类的实例存储在列表中,代码如下:

type
    TMyClass = class
    public
        name: String;
        old: Integer;
        anx: boolean;
    end;

...

function x(aList: TList): String;
var
    aObj: TMyClass;
    i: Integer;
begin
    for i:= 1 to aList.Count do
    begin
        aObj := aList[i-1];
    end;
end;

如何在 C# 中执行此操作?

如何在 C# 中编写

TList
等效项并让我的班级收到
TList

c# delphi tlist
2个回答
9
投票

C# 的等价物是通用列表容器

List<T>
。它与 Delphi 的
TList
非常相似,但由于使用了泛型,它是一个类型安全的容器。事实上,在现代 Delphi 代码中,由于类型安全性,泛型 Delphi 类
TList<T>
比非泛型
TList
更受青睐。

假设您想要一个

MyClass
对象列表,您可以实例化
List<MyClass>
的实例。

List<MyClass> list = new List<MyClass>();

然后您可以添加项目

list.Add(obj);

等等。


4
投票
//this is the class with fields.
public class TMyClass
{
    public String Name;
    public int old;
    public bool anx;
}

//this is the class with properties.
public class TMyClass
{
    public String Name { get; set; };
    public int old { get; set; };
    public bool anx { get; set; };
}

public string x(List<TMyClass> list)
{
    TMyClass aObj;
    for(int i = 0; i++; i < list.Count)
    {
        aObj = list[i];
    }
    //NEED TO RETURN SOMETHING?
}

这是您的类和函数的翻译。但我确实相信你的函数需要返回一些东西......

© www.soinside.com 2019 - 2024. All rights reserved.