在构造函数初始化列表 [英] Initializing lists in a constructor
问题描述
我需要创建一个类,还初始化两个事件列表,以新的空列表。我不知道这是什么被问过我,但我知道如何创建一个列表,以及如何创建一个构造函数。我创建了2列出了,现在我应该创建构造函数。这里是我的列表中的一个:
I need to create a class that also initializes two event lists to new empty lists. I'm not sure if that is what is being asked of me, but I know how to create a list and how to create a constructor. I created 2 lists, and now I should create the constructor. Here is one of my lists:
List<Person> organize = new List<Person>();
我如何初始化这两个事件列表在构造新的列表?
How do I initialize the two event lists in the constructor to new lists?
推荐答案
根据我可以从你的问题收集,你有两个列表的类。
Based on what I can gather from your question, you have a class with two Lists.
您的要求说你的类中,你需要在列表初始化为空列表。
Your requirements say that inside your class, you need to initialize the Lists to empty lists.
下面的例子中(唯一的区别是,我从来没有初始化列表,当他们宣布,但在类的构造函数代替):
Below is the example (the only difference is that I never initialize the Lists when they declared, but in the class constructor instead):
public class YourClass
{
List<Person> organize;
List<Person> anotherOrganize;
// constructor
public YourClass()
{
// initialize the two lists to empty lists
organize = new List<Person>();
anotherOrganize = new List<Person>();
}
}
这篇关于在构造函数初始化列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!