[英]How do I create an ArrayList of objects from another class in C#
I am trying to create an ArrayList of instances from another class in my program.我正在尝试从我的程序中的另一个类创建实例的 ArrayList。 In my case the class I am trying to create an ArrayList of is called record with the parameters firstname, lastname, and email.
在我的例子中,我试图创建一个 ArrayList 的类被称为记录,参数为 firstname、lastname 和 email。 I have created another class called emailList and I am trying to instantiate the array list there.
我创建了另一个名为 emailList 的类,我试图在那里实例化数组列表。 I am getting the error "The non-generic type 'ArrayList' cannot be used with type arguments".
我收到错误“非泛型类型 'ArrayList' 不能与类型参数一起使用”。
Here is the code I have to create record objects in the Record class:这是我必须在 Record 类中创建记录对象的代码:
public class Record
{
private String firstname;
private String lastname;
private String email;
public Record(String firstNameIn, String lastNameIn, String emailIn)
{
firstname = firstNameIn;
lastname = lastNameIn;
email = emailIn;
}
In the emailList class I am trying to create an ArrayList in the emailList class using:在 emailList 类中,我尝试使用以下方法在 emailList 类中创建一个 ArrayList:
ArrayList<Record> eList = new ArrayList<Record>;
ArrayList
is not generic meaning you cannot provide an generic argument (eg: ArrayList<Record>
), you need to instantiate it as var eList = new ArrayList();
ArrayList
不是通用的,这意味着您不能提供通用参数(例如: ArrayList<Record>
),您需要将其实例化为var eList = new ArrayList();
For type-safe collection check List<T>
under System.Collections.Generic
namespace.对于类型安全的集合,检查
System.Collections.Generic
命名空间下的List<T>
。
You don't need a type specifier for ArrayList
:您不需要
ArrayList
的类型说明符:
ArrayList eList = new ArrayList();
eList.Add(new Record("Test", "Test", "test@test.com"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.